Skip to content

Instantly share code, notes, and snippets.

@torgeir
Last active July 14, 2018 11:06
Show Gist options
  • Save torgeir/7618372 to your computer and use it in GitHub Desktop.
Save torgeir/7618372 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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