Last active
August 29, 2015 14:07
-
-
Save apaleslimghost/1e582a08b22a2888291a to your computer and use it in GitHub Desktop.
Generators for do-notation
This file contains hidden or 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
var sequence = (M, ms)=> ms.reduce( | |
(m1, m2)=> do(M, function*() { | |
var x = yield m1; | |
var xs = yield m2; | |
return x.concat(xs); | |
}), M.of([])); | |
// should be eqv to | |
var sequence = (M, ms)=> ms.reduce( | |
(m1, m2)=> { | |
m1.chain((x) => { | |
m2.chain((xs) => { | |
return M.of(x.concat(xs)); | |
}}}, M.of([])); | |
function doom(M, fn) { | |
var g = fn(); | |
var loop = (x)=> { | |
var {value, done} = g.next(x); | |
return done ? M.of(value) : value.chain(loop); | |
}; | |
return loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment