type Section (lines:string list) = member x.Lines = lines /// Splits a sequence after the last line of a section defined by the predicate. let splitAtEach (predicate: 'T -> bool) (xs : seq<'T>) : seq<'T list> = let section = new ResizeArray<'T>() seq { for x in xs do section.Add x if predicate x then yield Seq.toList section section.Clear() if section.Count > 0 then yield Seq.toList section} /// Splits a sequence before the first line of a section defined by the predicate. let splitBeforeEach (predicate: 'T -> bool) (xs : seq<'T>) : seq<'T list> = let section = new ResizeArray<'T>() seq { for x in xs do if predicate x then yield Seq.toList section section.Clear() section.Add x if section.Count > 0 then yield Seq.toList section}
usage:
open System.IO let lines = File.ReadAllLines(myTextFile) let getSections lines = splitAtEach isSectionLine lines |> Seq.map (fun x -> new Section(x)) |> Seq.toList
This is a slightly modified version from: https://codereview.stackexchange.com/questions/55554/parsing-sections-from-a-log-file-using-f