Skip to content

Instantly share code, notes, and snippets.

@caasi
Last active December 20, 2015 14:28
Show Gist options
  • Save caasi/6146476 to your computer and use it in GitHub Desktop.
Save caasi/6146476 to your computer and use it in GitHub Desktop.
use function as object
# Maybe monad implementation:
m = (value) ->
(f) ->
| f => f value # as bind
| otherwise => value # as unwrap
Nothing = -> Nothing
Just = m
# For convenience
unit = Just
nothing = Nothing
just = Just
# Demonstration on how to use Maybe monad:
test = []
test.push(
do do
x <- (just 5)
x <- (unit x + 1)
unit 1 - x
)
test.push(
do do
x <- nothing
x <- (unit x + 1)
unit 1 - x
)
console.log(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment