Created
December 3, 2013 00:52
-
-
Save danclien/7762014 to your computer and use it in GitHub Desktop.
Option monad in JavaScript without using an Array.
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.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