Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An es6 js maybe monad, using generators
log = console.log.bind(console);
maybe = v => {
function * f () {
if (v) yield v;
}
f.bind = f =>
v ? f(v) : maybe();
f.map = f =>
v ? maybe(f(v)) : maybe();
f.toString = () => `[Maybe ${v}]`;
return f;
};
maybe.lift = f =>
v => maybe(f(v))
maybe.lift2 = f =>
(m1, m2) => maybe(
m1.bind(v1 =>
m2.bind(v2 =>
f(v1, v2))));
let one = maybe('1').map(Number);
log(one); // [Maybe 1]
let none = maybe().map(Number);
log(none); // [Maybe undefined]
let addOne = x => x + 1;
let two = one.bind(maybe.lift(addOne));
let stillnone = none.bind(maybe.lift(addOne));
for (let val of two()) log(val); // 2
for (let val of stillnone()) log(val); // does not run
let add = (x, y) => x + y;
let addMaybe = maybe.lift2(add);
let three = addMaybe(one, two);
let evenstillnone = addMaybe(one, none);
for (let val of three()) log(val); // 3
for (let val of evenstillnone()) log(val); // does not run
@torgeir

This comment has been minimized.

Copy link
Owner Author

@torgeir torgeir commented Jan 27, 2017

Bind should probably return v, not maybe().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment