Skip to content

Instantly share code, notes, and snippets.

@ebiggs
Last active December 12, 2015 04:28
Show Gist options
  • Save ebiggs/4714565 to your computer and use it in GitHub Desktop.
Save ebiggs/4714565 to your computer and use it in GitHub Desktop.
basic IO Monad in coffeescript
IO = (f) ->
ioF = -> f()
ioF.bind = (getsNextIoF) -> IOChain(ioF, getsNextIoF)
ioF
IOChain = (ioF, getNextIoF) ->
ioCF = ->
value = ioF()
nextIoF = getNextIoF(value)
nextIoF()
ioCF.bind = (g) -> IOChain(ioCF, g)
ioCF
fAlert = (str) -> IO(-> alert(str))
fConfirm = IO(-> confirm("Well?"))
ioY = fAlert("THIS IS AN IO MONAD!").bind ->
fAlert("farewell, my dear friend :)")
ioN = fAlert("really? you suck.").bind ->
fAlert("buzz off.")
io = fAlert("hello!").bind ->
fAlert("So you want to know what's crazy?").bind -> \
fConfirm.bind (yn) -> \
(if yn then ioY else ioN)
io()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment