lundi 12 octobre 2015

F# Seq.next - what's the correct pattern?

As part of a project that uses the Strategy Pattern, I'm trying to write a function, that creates a function, which returns the next value of an infinite sequence each time it is applied. At the moment I am doing it using this dodgy GetNext function:

let GetNext<'T> (enumerator:System.Collections.Generic.IEnumerator<'T>) =
    let n = enumerator.MoveNext()
    enumerator.Current

let FunctionFactory<'T> =
    let s = 0.0 |> Seq.unfold (fun i -> Some(i, if 0.0 = i then 1.0 else 0.0))
    let enumerator = s.GetEnumerator()
    (fun (ignoredParam:'T) -> GetNext enumerator )

I would like FunctionFactory to look like this:

let FunctionFactory<'T> =
    let s = 0.0 |> Seq.unfold (fun i -> Some(i, if 0.0 = i then 1.0 else 0.0))
    (fun (ignoredParam:'T) -> Seq.next enumerator )

The ignoredParam is used in other functions that pass through the same strategy pattern and depend on the context it provides. Since this looks so bad, really I have two questions. Why isn't there a Seq.next? What is the correct/elegant way of implementing this?

Aucun commentaire:

Enregistrer un commentaire