Skip to content

Instantly share code, notes, and snippets.

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