Monday, 15 April 2013

Reading Grouped Data

Having multiple records of information stored as plain text in files is certainly neither uncommon or difficult to read back in. Grouped lines however, was a problem I came across a while ago, with data being split across multiple lines and being separated by a line of empty text. My first shot at this worked fine:

 let ProcessFile (allLines: string list) =   
   let list = new List<List<string>>()  
   let rec SplitFile (input: string list) =  
     if input.Length <> 0 then  
       list.Add(new List<string>(input.TakeWhile(fun x -> x <> "")))  
       let nextGroup = input.SkipWhile(fun x -> x <> "").SkipWhile(fun x -> x = "")  
       SplitFile (Seq.toList nextGroup)  
   SplitFile allLines |> ignore  

But I was unhappy with this solution. It doesn’t seem idiomatic to new List<List<string>> and store the data in there. So I took to StackOverflow in search of a better answer, which was soon provided:
Which directed me to what I wanted, a solution that retained the use of immutable data.

No comments:

Post a Comment

Google Developer Group MK at Bletchley Park

The Milton Keynes GDG hosted their December meetup at The National Museum of Computing inside Bletchley Park. We had a detailed demons...