Skip to content

Instantly share code, notes, and snippets.

@leemour
Forked from andyhd/maybe-monad.js
Created June 28, 2013 15:27
Show Gist options
  • Save leemour/5885550 to your computer and use it in GitHub Desktop.
Save leemour/5885550 to your computer and use it in GitHub Desktop.
function maybe(value) {
var obj = null;
function isEmpty() { return value === undefined || value === null }
function nonEmpty() { return !isEmpty() }
obj = {
map: function (f) { return isEmpty() ? obj : maybe(f(value)) },
getOrElse: function (n) { return isEmpty() ? n : value },
isEmpty: isEmpty,
nonEmpty: nonEmpty
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment