Skip to content

Instantly share code, notes, and snippets.

@rajaraodv
Created November 9, 2016 00:28
Show Gist options
  • Save rajaraodv/bf97c11ccd2c09f7bff2caf519acb2ed to your computer and use it in GitHub Desktop.
Save rajaraodv/bf97c11ccd2c09f7bff2caf519acb2ed to your computer and use it in GitHub Desktop.
A sample implementation of Monad
//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