1 people like it.

Partition a sequence into groups linearly by predicate

Partitions a sequence into groups linearly by predicate. I use this for breaking up my lazy record parsing with sequences into entity-sized chunks which are then easily digestible. Note: Edited back from the previous edit as these were heavily profiled and yield! tends to be slow. Edit #2: Now correctly using "use" instead of "let" for sequence.GetEnumerator () (Thanks Vladimir Matveev)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
let partition inclusive f (xs : #seq<_>) =
      let iter = xs.GetEnumerator()
      let rec loop () =
         seq {
            let rec innerLoop more =
               [
                  if more then
                     if not <| f iter.Current then
                        yield iter.Current
                        yield! innerLoop (iter.MoveNext())
                     elif inclusive then
                        yield iter.Current
               ]
            while iter.MoveNext() do
               yield innerLoop true
         }
      loop ()

module Seq =
    /// Partition into groups linearly by predicate (drops the partition element)
    /// ex. partitionLinear (fun x -> x = 1) [2; 3; 4; 5; 1; 2; 3; 4; 5]
    /// val it : seq<int list> = seq [[2; 3; 4; 5]; [2; 3; 4; 5]]
    let partitionLinear f xs = partition false f xs
    /// Partition into groups linearly by predicate (partition element inclusive)
    /// ex. partitionLinearInclusive (fun x -> x = 1) [2; 3; 4; 5; 1; 2; 3; 4; 5]
    /// val it : seq<int list> = seq [[2; 3; 4; 5; 1]; [2; 3; 4; 5]]
    let partitionLinearInclusive f xs = partition true f xs
val partition : inclusive:bool -> f:('a -> bool) -> xs:#seq<'a> -> seq<'a list>

Full name: Script.partition
val inclusive : bool
val f : ('a -> bool)
val xs : #seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val iter : System.Collections.Generic.IEnumerator<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
val loop : (unit -> seq<'a list>)
val innerLoop : (bool -> 'a list)
val more : bool
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
property System.Collections.Generic.IEnumerator.Current: 'a
System.Collections.IEnumerator.MoveNext() : bool
Multiple items
module Seq

from Script

--------------------
module Seq

from Microsoft.FSharp.Collections
val partitionLinear : f:('a -> bool) -> xs:seq<'a> -> seq<'a list>

Full name: Script.Seq.partitionLinear


 Partition into groups linearly by predicate (drops the partition element)
 ex. partitionLinear (fun x -> x = 1) [2; 3; 4; 5; 1; 2; 3; 4; 5]
 val it : seq<int list> = seq [[2; 3; 4; 5]; [2; 3; 4; 5]]
val xs : seq<'a>
val partitionLinearInclusive : f:('a -> bool) -> xs:seq<'a> -> seq<'a list>

Full name: Script.Seq.partitionLinearInclusive


 Partition into groups linearly by predicate (partition element inclusive)
 ex. partitionLinearInclusive (fun x -> x = 1) [2; 3; 4; 5; 1; 2; 3; 4; 5]
 val it : seq<int list> = seq [[2; 3; 4; 5; 1]; [2; 3; 4; 5]]

More information

Link:http://fssnip.net/4q
Posted:13 years ago
Author:Rick Minerich
Tags: seq , sequences , collections , grouping