Last active
December 17, 2015 22:49
-
-
Save SPY/5684674 to your computer and use it in GitHub Desktop.
Simple Maybe implementation
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(window) { | |
function Some(v) { | |
this.value = v; | |
} | |
Some.prototype = { | |
map: function(f) { | |
return some(f(this.value)); | |
}, | |
isNone: false, | |
fromMaybe: function() { | |
return this.value; | |
}, | |
fromSome: function() { | |
return this.value; | |
} | |
} | |
function None() {} | |
None.prototype = { | |
map: function() { | |
return none; | |
}, | |
isNone: true, | |
fromMaybe: function(x) { | |
return x; | |
}, | |
fromSome: function() { | |
throw new Error('It is not nothing'); | |
} | |
} | |
var none = window.none = new None(); | |
var some = window.some = function(x) { return new Some(x) }; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment