Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created January 17, 2013 01:33
Show Gist options
  • Select an option

  • Save dtchepak/4552765 to your computer and use it in GitHub Desktop.

Select an option

Save dtchepak/4552765 to your computer and use it in GitHub Desktop.
exports.OptionMonad = {
bind: function(f, m) {
if (m.isEmpty) return m;
else return f(m.value);
},
unit: function(x) { return new exports.Option(x); },
}
exports.Option = (function() {
return function(x) {
var is_empty = typeof x == 'undefined' || x == null
return { value: x, isEmpty: is_empty, toString: function() {
if (is_empty) return "{Empty}";
else return "{Just " + x + "}";
} };
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment