3 people like it.

tryNth

Computes the nth value of a sequence if the nth value is past the length of the sequence then None is returned else some

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
let tryNth index (source : seq<_>) = 
    let rec tryNth' index (e : System.Collections.Generic.IEnumerator<'a>) = 
        if not (e.MoveNext()) then None
        else if index < 0 then None
        else if index = 0 then Some(e.Current)
        else tryNth' (index-1) e

    use e = source.GetEnumerator()
    tryNth' index e
val tryNth : index:int -> source:seq<'a> -> 'a option

Full name: Script.tryNth
val index : int
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 tryNth' : (int -> System.Collections.Generic.IEnumerator<'a> -> 'a option)
val e : System.Collections.Generic.IEnumerator<'a>
namespace System
namespace System.Collections
namespace System.Collections.Generic
type IEnumerator<'T> =
  member Current : 'T

Full name: System.Collections.Generic.IEnumerator<_>
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
System.Collections.IEnumerator.MoveNext() : bool
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
property System.Collections.Generic.IEnumerator.Current: 'a
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
Raw view New version

More information

Link:http://fssnip.net/c0
Posted:5 years ago
Author:Colin Bull
Tags: sequences