Skip to content

Instantly share code, notes, and snippets.

@danclien
Created December 3, 2013 00:52
Show Gist options
  • Save danclien/7762014 to your computer and use it in GitHub Desktop.
Save danclien/7762014 to your computer and use it in GitHub Desktop.
Option monad in JavaScript without using an Array.
function Some(value) {
this.value = value;
}
Some.prototype.bind = function(f) {
return f(this.value);
}
function None() {
}
None.prototype.bind = function(f) {
return this;
}
var a = new Some(42);
var b = new None;
var c = new Some(100);
a.bind(function(valueA) {
return b.bind(function(valueB) {
return c.bind(function(valueC) {
return new Some(valueA + valueB + valueC);
})
})
});
a.bind(function(valueA) {
return c.bind(function(valueC) {
return new Some(valueA + valueC);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment