Skip to content

Instantly share code, notes, and snippets.

@tomachalek
Created July 9, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomachalek/3521752425f6c6d81e13f40b5898e468 to your computer and use it in GitHub Desktop.
Save tomachalek/3521752425f6c6d81e13f40b5898e468 to your computer and use it in GitHub Desktop.
function Nothing() {}
function Just(x) {
this.x = x;
}
function divide(a, b) {
if (typeof a !== 'number' || typeof b !== 'number' || b === 0) {
return new Nothing();
} else {
return new Just(a / b);
}
}
function unit(x) {
if (x === null || x === undefined) {
return new Nothing();
} else {
return new Just(x);
}
}
function bind(mx, fn) {
if (mx instanceof Nothing) {
return new Nothing();
} else {
return fn(mx.x);
}
}
function mdivide(ma, mb, mc) {
return bind(ma, a => {
return bind(mb, b => {
return bind(divide(a, b), tmp => {
return bind(mc, c => {
return divide(tmp, c);
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment