Skip to content

Instantly share code, notes, and snippets.

@andyhd
Created January 16, 2012 01:02
Show Gist options
  • Save andyhd/1618403 to your computer and use it in GitHub Desktop.
Save andyhd/1618403 to your computer and use it in GitHub Desktop.
Maybe monad in Javascript
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;
}
@jhegedus42
Copy link

@Muzietto, lol ! how true :)

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