[Haskell-cafe] How to convert records into Haskell structure in
Haskell way?
Daniel Fischer
daniel.is.fischer at web.de
Mon Nov 2 20:55:45 EST 2009
Am Dienstag 03 November 2009 02:29:56 schrieb Magicloud Magiclouds:
> Hi,
> Say I have something like this:
> [ Record { item = "A1", value = "0" }
> , Record { item = "B1", value = "13" }
> , Record { item = "A2", value = "2" }
> , Record { item = "B2", value = "10" } ]
> How to convert it into:
> [ XXInfo { name = "A", value1 = "0", value2 = "2" }
> , XXInfo { name = "B", value1 = "13", value2 = "10" } ]
> If XXInfo has a lot of members. And sometimes the original data
> might be not integrity.
Could you be a little more specific about what you want to achieve?
As a first guess, you might use something like
import Data.List
import Data.Ord (comparing)
import Data.Function (on)
sortedRecords = sortBy (comparing item) records
recordGroups = groupBy ((==) `on` (head . item)) sortedRecords
-- now comes the tricky part, converting the groups to XXinfo
-- if all groups are guaranteed to have the appropriate number of
-- elements, you can use
xxInfo [Record (c:_) v1, Record _ v2] = XXinfo [c] v1 v2
-- and then
xxInfos = map xxInfo recordGroups
-- if the groups may have different numbers of elements, it's going to be uglier
More information about the Haskell-Cafe
mailing list