0 people like it.

Expose 'stdin' as a sequence of ints with termination control.

Some scenarios require the ability to read stdin as a sequence of character codes. The console in .Net does not behave the same as a file. The console also has limited buffering capacity (you can't paste a large string into the console for example - try it). Call this function with arg = [-1;13] and the sequence will end if either 'enter' is pressed at the console or EOF is reached with a file.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
    /// <summary>
    /// Represents stdin as an infinite sequence of ints.
    /// </summary>
    /// <remarks>
    /// This function represents a sequence of ints that are read from 'stdin'. The sequence terminates whenever a value is read that is equal
    /// to any of the values in the 'terminators' argument. If stdin is mapped to the console then you can passs 13 as the 'enter' key code so that the
    /// sequence terminates when the user presses 'enter'. If stdin is mapped to a file then you can use -1 as the code that corresponds
    /// to the end of file code.
    /// </remarks> 
    /// <param name="chars">A list (which may be empty) of termination codes, any one of which will cause the sequence to end and excludes the termination code.</param>
    let inseq terminators = 
        let get_char chars =
            let code = Operators.stdin.Read()
            match chars with
            | _    when Seq.exists (fun x -> x = code) chars -> None
            | _ -> Some(char code, 0)

        (Seq.unfold (fun _ -> get_char terminators) 0) 
val inseq : terminators:seq<int> -> seq<char>

Full name: Script.inseq


 <summary>
 Represents stdin as an infinite sequence of ints.
 </summary>
 <remarks>
 This function represents a sequence of ints that are read from 'stdin'. The sequence terminates whenever a value is read that is equal
 to any of the values in the 'terminators' argument. If stdin is mapped to the console then you can passs 13 as the 'enter' key code so that the
 sequence terminates when the user presses 'enter'. If stdin is mapped to a file then you can use -1 as the code that corresponds
 to the end of file code.
 </remarks>
 <param name="chars">A list (which may be empty) of termination codes, any one of which will cause the sequence to end and excludes the termination code.</param>
val terminators : seq<int>
val get_char : (seq<int> -> (char * int) option)
val chars : seq<int>
val code : int
module Operators

from Microsoft.FSharp.Core
val stdin<'T> : System.IO.TextReader

Full name: Microsoft.FSharp.Core.Operators.stdin
module Seq

from Microsoft.FSharp.Collections
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.exists
val x : int
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold

More information

Link:http://fssnip.net/7Qo
Posted:10 months ago
Author:Hugh Gleaves
Tags: io