Skip to content

Instantly share code, notes, and snippets.

@SPY
Last active December 17, 2015 22:49
Show Gist options
  • Save SPY/5684674 to your computer and use it in GitHub Desktop.
Save SPY/5684674 to your computer and use it in GitHub Desktop.
Simple Maybe implementation
(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