Skip to content

Instantly share code, notes, and snippets.

@WA9ACE
Created January 22, 2014 16:34
Show Gist options
  • Save WA9ACE/8562010 to your computer and use it in GitHub Desktop.
Save WA9ACE/8562010 to your computer and use it in GitHub Desktop.
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'
@evanrinehart
Copy link

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");
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment