Last active
December 12, 2015 04:28
-
-
Save ebiggs/4714565 to your computer and use it in GitHub Desktop.
basic IO Monad in coffeescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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