2 people like it.

Seq.triplewise

Triple version of Seq.pairwise.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
let triplewise (source: seq<_>) =
    seq { use e = source.GetEnumerator() 
        if e.MoveNext() then
            let i = ref e.Current
            if e.MoveNext() then
                let j = ref e.Current
                while e.MoveNext() do
                    let k = e.Current 
                    yield (!i, !j, k)
                    i := !j
                    j := k }

// sample
triplewise [1..4] // -> seq [(1, 2, 3); (2, 3, 4)]
val triplewise : source:seq<'a> -> seq<'a * 'a * 'a>

Full name: Script.triplewise
val source : 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 e : System.Collections.Generic.IEnumerator<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
System.Collections.IEnumerator.MoveNext() : bool
val i : 'a 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<_>
property System.Collections.Generic.IEnumerator.Current: 'a
val j : 'a ref
val k : 'a
Raw view Test code New version

More information

Link:http://fssnip.net/50
Posted:13 years ago
Author:ptan
Tags: sequences , seq