One way to do this is to use a basic one IEnumerator<String>. It's not exactly one-line, but it seems to be a little cleaner than yours. (Does not rely on mutable, uses .NET idioms correctly.)
Essentially, you get the IEnumerator <'> interface from the sequence, and then just go to the MoveNext call. This will work perfectly in endless sequence.
> let getNextFunc (seqOfLines : seq<'a>) =
- let linesIE : IEnumerator<'a> = seqOfLines.GetEnumerator()
- (fun () -> ignore (linesIE.MoveNext()); linesIE.Current);;
val getNextFunc : seq<'a> -> (unit -> 'a)
To use, just pass the getNextFuncsequence and it will return your next nextLine function.
> let sequenceOfStrings = seq { for i = 0 to 10000 do yield i.ToString() };;
val sequenceOfStrings : seq<string>
> let nextLine = getNextFunc sequenceOfStrings;;
val nextLine : (unit -> string)
> nextLine();;
val it : string = "0"
> nextLine();;
val it : string = "1"
> nextLine();;
val it : string = "2"
> nextLine();;
val it : string = "3"
source
share