Created
December 3, 2013 00:55
-
-
Save danclien/7762048 to your computer and use it in GitHub Desktop.
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
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