Skip to content

Instantly share code, notes, and snippets.

@eddieantonio
Last active December 30, 2015 15:09
Show Gist options
  • Save eddieantonio/7846815 to your computer and use it in GitHub Desktop.
Save eddieantonio/7846815 to your computer and use it in GitHub Desktop.
# 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