0 people like it.
Like the snippet!
Seq.groupAfter function
This snippet is basically the same as http://fssnip.net/6A, except that the element whose predicate holds ends the previous group and the element after it starts a new one.
Those two snippets (Seq.groupWhen, Seq.groupAfter) would be generally equivalent to the Haskell functions 'breakBefore' and 'breakAfter' from Data.List.Grouping.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
|
module Seq =
/// Iterates over the elements of the input sequence and groups adjacent
/// elements. A new group is started after the specified predicate holds
/// about the element of the sequence (and at the beginning of the iteration).
let groupAfter f (input:seq<_>) =
use en = input.GetEnumerator()
let rec group() =
[ yield en.Current
if not(f en.Current) && en.MoveNext() then
yield! group() ]
seq{
while en.MoveNext() do
yield group() |> Seq.ofList }
|
1:
2:
|
[3;3;2;4;1;2] |> Seq.groupAfter (fun n -> n%2 = 1);;
/// val it : seq<seq<int>> = seq [[3]; [3]; [2; 4; 1]; [2]]
|
module Seq
from Microsoft.FSharp.Collections
val groupAfter : f:('a -> bool) -> input:seq<'a> -> seq<seq<'a>>
Full name: Script.Seq.groupAfter
Iterates over the elements of the input sequence and groups adjacent
elements. A new group is started after the specified predicate holds
about the element of the sequence (and at the beginning of the iteration).
val f : ('a -> bool)
val input : 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 en : System.Collections.Generic.IEnumerator<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
val group : (unit -> 'a list)
property System.Collections.Generic.IEnumerator.Current: 'a
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
System.Collections.IEnumerator.MoveNext() : bool
val ofList : source:'T list -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.ofList
Multiple items
module Seq
from Script
--------------------
module Seq
from Microsoft.FSharp.Collections
val n : int
More information