2 people like it.

Skip that is safe when not enough lines

A version of Seq.skip which doesn't throw an exception if there are fewer lines in the sequence than you have asked to skip.

 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: 
28: 
29: 
module Seq =

    /// Skip the first n lines of the sequence, and don't throw an exception if there are fewer than n lines.
    let skipIf n s =
        s
        |> Seq.mapi (fun i elem -> i, elem)
        |> Seq.choose (fun (i, elem) -> if i >= n then Some(elem) else None)

module Examples = 

    let skipdemo01 =
        Seq.init 0 (fun x -> x)
        |> Seq.skipIf 1

    let skipdemo11 =
        Seq.init 1 (fun x -> x)
        |> Seq.skipIf 1

    let skipdemo02 =
        Seq.init 0 (fun x -> x)
        |> Seq.skipIf 2

    let skipdemo32 =
        Seq.init 3 (fun x -> x)
        |> Seq.skipIf 2

    let skipdemo30 =
        Seq.init 3 (fun x -> x)
        |> Seq.skipIf 0
module Seq

from Microsoft.FSharp.Collections
val skipIf : n:int -> s:seq<'a> -> seq<'a>

Full name: Script.Seq.skipIf


 Skip the first n lines of the sequence, and don't throw an exception if there are fewer than n lines.
val n : int
val s : seq<'a>
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val i : int
val elem : 'a
val choose : chooser:('T -> 'U option) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.choose
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
module Examples

from Script
val skipdemo01 : seq<int>

Full name: Script.Examples.skipdemo01
Multiple items
module Seq

from Script

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

from Microsoft.FSharp.Collections
val init : count:int -> initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.init
val x : int
val skipdemo11 : seq<int>

Full name: Script.Examples.skipdemo11
val skipdemo02 : seq<int>

Full name: Script.Examples.skipdemo02
val skipdemo32 : seq<int>

Full name: Script.Examples.skipdemo32
val skipdemo30 : seq<int>

Full name: Script.Examples.skipdemo30
Raw view Test code New version

More information

Link:http://fssnip.net/cH
Posted:12 years ago
Author:Kit Eason
Tags: sequences skip