[Haskell-cafe] Data creation pattern?
Eugene Dzhurinsky
bofh at redwerk.com
Thu May 13 11:34:26 EDT 2010
Hello, all!
I need to create objects like this
data Object = MyObject { param1, param2, param3 :: String }
from the input file
param_1_param1=value11
param_2_param1=value21
param_2_param2=value22
param_1_param3=value13
param_2_param3=value23
param_1_param2=value12
so general pattern of recognizing parameter name and value is
param_{id}_{property name}={property value}
so I need to create function
> parseDataFile :: [String] -> [Object]
For now I can think on splitting the task into 2 functions
> groupSameObjectParameters :: [String] -> [[String]]
which will group the lines with same id into a list, and then apply function
> createObject :: [String] -> Object
so overall solution will be
> parseDataFile :: [String] -> [Object]
> parseDataFile = map createObject . groupSameObjectParameters
however I have no neat idea about how to create instance of MyObject - I need
to supply all of parameters to the constructor at once.
Also I don't like the idea of rearranging list first and then create objects
from another list, because the list can be relatively large.
In imperative programming with mutable objects I would create an array, then
for each line get the id and try to find if there is the object in the array
at 'id' index. If no - create one. Then set appropriate property from value.
Is it possible to do something similar in Haskell?
Thank you in advance!
--
Eugene N Dzhurinsky
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100513/10265a41/attachment.bin
More information about the Haskell-Cafe
mailing list