Created
January 22, 2014 16:34
-
-
Save WA9ACE/8562010 to your computer and use it in GitHub Desktop.
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
Maybe = function(value) { | |
var Nothing = {}; | |
var Something = function(value) { | |
return function() { | |
return value; | |
}; | |
}; | |
if (typeof value === 'undefined' || value === null) | |
return Nothing; | |
return Something(value); | |
}; | |
Maybe(null) == Nothing; // true | |
typeof Maybe(null); // 'object' | |
Maybe('foo') == Nothing; // false | |
Maybe('foo')(); // 'foo' | |
typeof Maybe('foo'); // 'function' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function just(x){ return {just: x}; }
var nothing = {nothing: null};
function maybe(d, f, m){
if('nothing' in m){
return d;
}
elseif('just' in m){
return f(m.just);
}
else{
throw new Exception("invalid maybe value");
}
}
function >>=(m, f){
if('null' in m){
return nothing;
}
elseif('just' in m){
return f(m.just);
}
else{
throw new Exception("invalid maybe value");
}
}