Created
November 9, 2016 00:28
-
-
Save rajaraodv/bf97c11ccd2c09f7bff2caf519acb2ed to your computer and use it in GitHub Desktop.
A sample implementation of 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
//Monad - a sample implementation | |
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(); | |
}; | |
ap(someOtherMonad) {//Used to deal w/ multiple Monads | |
return someOtherMonad.map(this.__value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment