
On Thursday 13 May 2010 20:43:44, Eugeny N Dzhurinsky wrote:
On Thu, May 13, 2010 at 07:14:25PM +0100, Stephen Tetley wrote:
Hi Eugene
Is something like this close to what you want:
Not really. First of all, there're many properties, not 3. So it may end up with plenty of support (boilerplate) code.
Also, names of these parameters are not sortable. Of course I could make these names as another data type with deriving from Eq/Ord - but that increase complexity as well.
Original idea was
1) create Array 2) if line "param_N_name=value" appear, then 2.1) try to take object N from array 2.2) if no object exists - then create one 2.3) set the property "name" to "value" 2.4) put resulting object back into array 3) take next line and go to 2
so if it is possible to have partially initialized objects in Haskell,
If the fields aren't strict, there's no problem having data MyObject = MyObject { param1, param2, param3 :: String } emptyObject = MyObject undefined undefined undefined -- Boilerplate code, but you can let that write the computer for you update :: String -> MyObject -> String -> MyObject update "param1" obj val = obj{param1 = val} update "param2" obj val = obj{param2 = val} update "param3" obj val = obj{param3 = val} update _ obj _ = obj and start from emptyObject, updateing the fields as they come (if the fields aren't all Strings, read or parse the value). import qualified Data.Map as Map import Data.List (foldl') -- break all lines into: -- object ID (N) -- field to be set (name) -- value breakUpLine :: String -> (Int, String, String) -- construct a map ID -> object from the lines of the file construct :: [String] -> Map Int MyObject construct = foldl' updateMap (Map.empty) where updateMap mp (i, fld, val) = case Map.findWithDefault emptyObject i mp of obj -> Map.insert i (update fld obj val) mp
then this approach should work. If now - well, then replace creation of object with adding name/value pair to an array. And then create objects from those arrays.