I have a record defined like below:
data MyData = MyData
{ name :: String
, addr :: String
... a lot of other fields of String type
}
Next I want to create list of pairs (String, fieldName)
, something like this:
fields =
[ ("NAME", name)
, ("ADDRESS", addr)
, ... and for other fields
]
And finally I need a function which can get empty record of type MyData
and fill it dynamically field by field, like this:
initByStrings strs = foldl (\ d (x, y) -> d{y=(findIn x strs)}) emptyMyData fields
Is a behaviour like this possible in Haskell without long monotonic constructions like below?
...
lst = map (\ x -> findIn x strs) fields
f lst where
f (name:addr:...) = MyData name addr ...
Solution was created in this way:
List of fields has a function which updates corresponding field in record :
fields =
[ ("NAME", (\d x -> d{name=x}))
, ("ADDRESS", (\d x -> d{addr=x}))
, ... and for other fields
]
Function which initializes MyData
record looks like:
initByStrings strs = foldl (\ d (x, y) -> y d(findIn x strs)}) emptyMyData fields
So records fields can be updated one by one from foldl
using some external function wich resolver string value from string name of field from list.