2 people like it.

Continue sequence after applying Seq.takeWhile, skip, take, etc

Sometimes you need to take more elements from a sequence, e.g. look at https://stackoverflow.com/questions/12562327/how-to-do-seq-takewhile-one-item-in-f This solution allows fetching elements from the same sequence after applying takeWhile, skip, take and other sequence functions. However, need to remember to call function with right parameter: use 'true' to include current element into result sequence (after takeWhile for example)

 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: 
/// allows fetching elements from same sequence
let continueSeq  (xs: 'a seq) = 
    use en = xs.GetEnumerator()

    fun(includeCurrent: bool) ->
        let s = seq { while en.MoveNext() do yield en.Current }
        let c = seq { en.Current }
        if includeCurrent then
            Seq.append c s
        else
            s

/// usage example:
let a = seq [1; 2; 3; 4; 5; 6; 7]

let seqfun = continueSeq  a 
let s1 = seqfun(false) |> Seq.takeWhile((>) 3) // take 1 and 2, 3 is current
let s2 = seqfun(true) |> Seq.take(2)    // take 3 and 4
let s3 = seqfun(false) |> Seq.skip(1)   // skip 5

let s = 
    s1 
    |> Seq.append <| s2 
    |> Seq.append <| s3 
    |> Seq.toList

// s = [1; 2; 3; 4; 6; 7]
val continueSeq : xs:seq<unit> -> (bool -> seq<unit>)

Full name: Script.continueSeq


 allows fetching elements from same sequence
val xs : seq<unit>
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<unit>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<unit>
val includeCurrent : bool
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val s : seq<unit>
System.Collections.IEnumerator.MoveNext() : bool
property System.Collections.Generic.IEnumerator.Current: unit
val c : seq<unit>
module Seq

from Microsoft.FSharp.Collections
val append : source1:seq<'T> -> source2:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.append
val a : seq<int>

Full name: Script.a


 usage example:
val seqfun : (bool -> seq<unit>)

Full name: Script.seqfun
val s1 : seq<unit>

Full name: Script.s1
val takeWhile : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.takeWhile
val s2 : seq<unit>

Full name: Script.s2
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val s3 : seq<unit>

Full name: Script.s3
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val s : unit list

Full name: Script.s
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList

More information

Link:http://fssnip.net/7XL
Posted:4 years ago
Author:Ruslan Enikeev
Tags: infinite sequence , seqences , sequence , sequence expressions , sequences skip