Search code examples
f#consolestreamsequences

F# how to abstract Console.ReadLine() as string seq


I want to write a function to abstract Console.ReadLine() into a string seq

the seq should break when line = null

ConsoleLines(): unit -> string seq

To be used like this:

for line in ConsoleLines() do
    DoSomething line

How do you write this function?

Thanks


Solution

  • Seq.initInfinite (fun _ -> Console.ReadLine())