Last active
July 14, 2018 11:06
-
-
Save torgeir/7618372 to your computer and use it in GitHub Desktop.
An es6 js maybe monad, using generators
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bind should probably return v, not maybe().