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  
   list  

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: http://stackoverflow.com/questions/11225068/getting-a-list-of-lists-without-using-listliststring-in-f/11225989#11225989
Which directed me to what I wanted, a solution that retained the use of immutable data.