Last active
December 30, 2015 15:09
-
-
Save eddieantonio/7846815 to your computer and use it in GitHub Desktop.
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
# Maybe in Coffeescript | |
# Terminology from Haskell; API from Scala | |
# Inspired by https://gist.github.com/andyhd/1618403 | |
just = (value) -> | |
# Applying the monad will act as "map" by default | |
# To make people even lazier, 'this' is bound to the value anyway. | |
monad = (f) -> just f.call(value, value) | |
monad.getOrElse = -> value | |
monad.getOr = -> value | |
monad.isEmpty = -> false | |
monad | |
nothing = (-> | |
monad = (f) -> nothing | |
monad.getOrElse = (f) -> f() | |
monad.getOr = (other) -> other | |
monad.isEmpty = -> true | |
monad | |
)() | |
# The helper function/module object. | |
maybe = (value) -> | |
if value? | |
just value | |
else | |
nothing | |
maybe.just = just | |
maybe.nothing = nothing | |
module.exports = maybe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment