Skip to content

Instantly share code, notes, and snippets.

@ebiggs
Created February 16, 2013 17:23
Show Gist options
  • Save ebiggs/4967798 to your computer and use it in GitHub Desktop.
Save ebiggs/4967798 to your computer and use it in GitHub Desktop.
io monad redux
IO = (doFirst) ->
io = () -> doFirst()
io.bind = (f) -> IO(-> f(doFirst())())
io
ioAlert = (x) -> IO(-> alert(x))
ioConfirm = (x) -> IO(-> confirm(x))
ioPrompt = (x) -> IO(-> prompt(x))
main = ->
ioPrompt("What is your name?").bind (name) -> \
ioAlert("Hello #{name}.").bind () -> \
ioAlert("welcome")
main2 = ->
m = ioPrompt("What is your name?")
f = (name) -> ioAlert("Hello #{name}!")
g = () -> ioAlert("welcome")
m.bind(f).bind(g)
main3 = ->
m = ioPrompt("What is your name?")
f = (name) -> ioAlert("Hello #{name}!")
g = () -> ioAlert("welcome")
m.bind (name) -> f(name).bind(g)
main3()()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment