Skip to content

Instantly share code, notes, and snippets.

@dreamineering
Created February 1, 2014 07:49
Show Gist options
  • Select an option

  • Save dreamineering/8749383 to your computer and use it in GitHub Desktop.

Select an option

Save dreamineering/8749383 to your computer and use it in GitHub Desktop.
Maybe = function(value) {
var Nothing = {
bind: function(fn) {
return this;
},
isNothing: function() {
return true;
},
val: function() {
throw new Error("cannot call val() nothing");
},
maybe: function(def, fn) {
return def;
}
};
var Something = function(value) {
return {
bind: function(fn) {
return Maybe(fn.call(this, value));
},
isNothing: function() {
return false;
},
val: function() {
return value;
},
maybe: function(def, fn) {
return fn.call(this, value);
}
};
};
if (typeof value === 'undefined' || value === null)
return Nothing;
return Something(value);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment