Skip to content

Instantly share code, notes, and snippets.

@tiye
Last active December 18, 2015 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiye/5746958 to your computer and use it in GitHub Desktop.
Save tiye/5746958 to your computer and use it in GitHub Desktop.
Maybe = (@value) ->
Maybe::ret = -> @value
Maybe::bind = (fn) ->
if @value? then (fn @value) else undefined
Maybe.lift = (fn) -> (val) -> new Maybe (fn val)
addOne = (val) -> val + 1
maybeAddOne = Maybe.lift addOne
Maybe.lift2 = (fn) -> (M1, M2) ->
helper = M1.bind (val1) ->
M2.bind (val2) ->
fn val1, val2
new Maybe helper
add = (a, b) -> a + b
m1 = new Maybe 1
m2 = new Maybe 2
m3 = new Maybe undefined
liftM2Add = Maybe.lift2 add
console.log liftM2Add(m1, m2).ret()
console.log liftM2Add(m3, m2).ret()
console.log liftM2Add(m1, m3).ret()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment