Skip to content

Instantly share code, notes, and snippets.

@jaredwy
Created May 18, 2013 22:51
Show Gist options
  • Save jaredwy/5606014 to your computer and use it in GitHub Desktop.
Save jaredwy/5606014 to your computer and use it in GitHub Desktop.
Maybe code.
function Maybe(x) {
this.x = x;
this.isEmpty = x == null || x == undefined;
}
Maybe.prototype.map = function(f) {
//composition operator anyone?
return this.chain(function(x) {
return Maybe.of(f(x));
});
}
Maybe.prototype.chain = function(f) {
if(this.isEmpty) return this;
return f(this.x);
}
Maybe.of = function(x) {
return new Maybe(x);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment