2 people like it.

Select n elements in group from seq

Select n elements in group from seq

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
open System.Collections.Generic

let seqTake n (s:'a IEnumerator) = 
    let rec loop n = 
        seq {
            if n > 0 && s.MoveNext() then yield s.Current; yield! loop (n-1)
        }
    loop n

let seqWin n (s:'a seq) =
    let e = s.GetEnumerator()
    let rec loop () = 
        seq {
            let lst = e |> seqTake n  |> Seq.toList
            if lst.Length <= n && lst.Length > 0 then 
                yield lst            
                yield! loop() 
        }
    loop ()

[1..5] |> seqWin 2
namespace System
namespace System.Collections
namespace System.Collections.Generic
val seqTake : n:int -> s:IEnumerator<'a> -> seq<'a>

Full name: Script.seqTake
val n : int
val s : IEnumerator<'a>
type IEnumerator<'T> =
  member Current : 'T

Full name: System.Collections.Generic.IEnumerator<_>
val loop : (int -> seq<'a>)
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
System.Collections.IEnumerator.MoveNext() : bool
property IEnumerator.Current: 'a
val seqWin : n:int -> s:seq<'a> -> seq<'a list>

Full name: Script.seqWin
val s : seq<'a>
val e : IEnumerator<'a>
IEnumerable.GetEnumerator() : IEnumerator<'a>
val loop : (unit -> seq<'a list>)
val lst : 'a list
module Seq

from Microsoft.FSharp.Collections
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
property List.Length: int
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/51
Posted:13 years ago
Author:Ankur Dhama
Tags: sequence