72 people like it.

Break sequence into n-element subsequences

I'm working on parallel computations and I thought it would be useful to break work into chunks, especially when processing each element asynchronously is too expensive. The neat thing is that this function is general even though motivation for it is specific. Another neat thing is that this is true lazy sequence unlike what you'd get if you used Seq.groupBy. There are three versions for your enjoyment.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
let tuple2 x y = x, y
module Seq=
    let trim n = Seq.map snd << Seq.filter (fst >> (<=) n) << Seq.mapi tuple2
    let breakBy n s = 
        let rec loop s = 
            seq { if not (Seq.isEmpty s) then 
                    yield (s |> Seq.truncate n)
                    yield! loop (s |> trim n) }
        loop s

seq {1..25000} |> Seq.breakBy 50
(*
val it : seq<seq<int>> =
  seq
    [seq [1; 2; 3; 4; ...]; seq [51; 52; 53; 54; ...];
     seq [101; 102; 103; 104; ...]; seq [151; 152; 153; 154; ...]; ...]
*)
val tuple2 : x:'a -> y:'b -> 'a * 'b

Full name: Script.tuple2
val x : 'a
val y : 'b
module Seq

from Microsoft.FSharp.Collections
val trim : n:int -> (seq<'a> -> seq<'a>)

Full name: Script.Seq.trim
val n : int
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val breakBy : n:int -> s:seq<'a> -> seq<seq<'a>>

Full name: Script.Seq.breakBy
val s : seq<'a>
val loop : (seq<'b> -> seq<seq<'b>>)
val s : seq<'b>
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 not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val isEmpty : source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.isEmpty
val truncate : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.truncate
Multiple items
module Seq

from Script

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

from Microsoft.FSharp.Collections

More information

Link:http://fssnip.net/1o
Posted:13 years ago
Author:Dmitri Pavlenkov
Tags: seq