Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f# equivalent of Haskell's "interact"

Tags:

haskell

f#

I was just reading a little about Haskell and saw its method interact which implements the common pattern of reading input from stdin, applying a function to the strings, and writing the result back to stdout. So, for example:

interact (map toUpper)

prints back everything that comes on stdin converted to uppercase.

Is there an equivalent of this already in F#/.Net?

like image 979
PCB Avatar asked Feb 05 '13 00:02

PCB


1 Answers

This functionality does not exist natively, but implementing it is easy:

let inline interact f =
    printfn "%s" (f (System.Console.Readline()))
like image 160
John Palmer Avatar answered Nov 07 '22 13:11

John Palmer