Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active August 29, 2015 14:07
Show Gist options
  • Save apaleslimghost/1e582a08b22a2888291a to your computer and use it in GitHub Desktop.
Save apaleslimghost/1e582a08b22a2888291a to your computer and use it in GitHub Desktop.
Generators for do-notation
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