Skip to content

Instantly share code, notes, and snippets.

@EugeneN
Created December 12, 2012 12:42
Show Gist options
  • Save EugeneN/4267444 to your computer and use it in GitHub Desktop.
Save EugeneN/4267444 to your computer and use it in GitHub Desktop.
Continuation monad. Working example: http://jsfiddle.net/n6GHF/10/
# http://jsfiddle.net/n6GHF/10/
say = (m...) ->
document.write "<div>Got result: #{m.join ', '}</div>"
console.log m...
identity = (x) -> x
log_result = (x...) -> say x...
cont_m =
result: (v) ->
(c) -> c v
bind: (mv, f) ->
(c) ->
mv ((v) -> ((f v) c))
run = (c) -> c log_result
lift_sync1 = (f, delay) ->
(x) ->
(c) ->
setTimeout(
-> c (f x)
delay
)
e1 = (x) -> x * x
e2 = (x) -> x + 2
e3 = (x) -> x + 0.25
f1 = lift_sync1 e1, 100
f2 = lift_sync1 e2, 200
f3 = lift_sync1 e3, 300
domonad = ({result, bind}, functions, init_value) ->
f0 = bind (result init_value), functions[0]
([f0].concat functions[1...]).reduce (a, b) ->
bind a, b
#z = bind (bind (bind (result 3), f3), f2), f1
y = domonad cont_m, [f1, f2, f3, f1], 33
#run y
y say
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment