Skip to content

Instantly share code, notes, and snippets.

@davidklassen
Created June 1, 2013 14:27
Show Gist options
  • Save davidklassen/5690588 to your computer and use it in GitHub Desktop.
Save davidklassen/5690588 to your computer and use it in GitHub Desktop.
Maybe monad implemented in javascript
function Maybe(value) {
this.value = value;
}
Maybe.Nothing = {};
Maybe.pull = function (value) {
return new Maybe(value);
}
Maybe.prototype.bind = function (fn) {
if (this.value === Maybe.Nothing) {
return Maybe.pull(Maybe.Nothing);
} else {
return fn(this.value);
}
}
function fromMaybe(fail, maybe) {
if (maybe.value === Maybe.Nothing) {
return fail();
} else {
return maybe.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment