Created
April 27, 2018 09:38
-
-
Save sharpred/ec2ed54e74411a0333a06bb0fe1436b1 to your computer and use it in GitHub Desktop.
simple monad
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
class Monad { | |
constructor(val) { | |
this.__value = val; | |
} | |
static of(val) {//Monad.of is simpler than "new Monad(val)" | |
return new Monad(val); | |
}; | |
map(f) {//Applies the function but returns another Monad! | |
return Monad.of(f(this.__value)); | |
}; | |
join() { // used to get the value out of the Monad | |
return this.__value; | |
}; | |
chain(f) {//Helper func that maps and then gets the value out | |
return this.map(f).join(); | |
}; | |
} | |
module.exports = Monad; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment