Created
May 18, 2013 22:51
-
-
Save jaredwy/5606014 to your computer and use it in GitHub Desktop.
Maybe code.
This file contains hidden or 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 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