Created
January 16, 2012 01:02
-
-
Save andyhd/1618403 to your computer and use it in GitHub Desktop.
Maybe monad in Javascript
This file contains 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
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; | |
} |
@andyhd just created something similar to this for node called Giftbox here. Would love to hear your thoughts/feedback! Also motivated by Scala :)
@Muzietto, lol ! how true :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is just a functor no?