Last active
December 20, 2015 14:28
-
-
Save caasi/6146476 to your computer and use it in GitHub Desktop.
use function as object
This file contains hidden or 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 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