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: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
open System
open System.Linq

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 (func : _ -> bool) (sequence : _ seq) : _ list seq =
        seq {
            use en = sequence.GetEnumerator ()
            let more = ref true
            while !more do
                let wasGood = ref true
                let sublist = 
                    [
                        while !wasGood && en.MoveNext() do
                            if not (func en.Current) then yield en.Current
                            else wasGood := false
                    ]
                if List.isEmpty sublist then more := false
                else yield sublist
        }

    /// 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 (func : _ -> bool) (sequence : _ seq) : _ list seq =
        seq {
            use en = sequence.GetEnumerator ()
            let more = ref true
            while !more do
                let wasGood = ref true
                let sublist = 
                    [
                        while !wasGood && en.MoveNext() do
                            if func en.Current then wasGood := false
                            yield en.Current
                    ]
                if List.isEmpty sublist then more := false
                else yield sublist
        }
namespace System
namespace System.Linq
Multiple items
module Seq

from Script

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

from Microsoft.FSharp.Collections
val partitionLinear : func:('a -> bool) -> sequence: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 func : ('a -> bool)
type bool = Boolean

Full name: Microsoft.FSharp.Core.bool
val sequence : seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

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

Full name: Microsoft.FSharp.Collections.seq<_>
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val en : Collections.Generic.IEnumerator<'a>
Collections.Generic.IEnumerable.GetEnumerator() : Collections.Generic.IEnumerator<'a>
val more : bool ref
Multiple items
val ref : value:'T -> 'T ref

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

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val wasGood : bool ref
val sublist : 'a list
Collections.IEnumerator.MoveNext() : bool
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
property Collections.Generic.IEnumerator.Current: 'a
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val isEmpty : list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.isEmpty
val partitionLinearInclusive : func:('a -> bool) -> sequence: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:12 years ago
Author:Rick Minerich
Tags: seq , sequences , collections , grouping