Skip to content

Instantly share code, notes, and snippets.

@sharpred
Created April 27, 2018 09:38
Show Gist options
  • Save sharpred/ec2ed54e74411a0333a06bb0fe1436b1 to your computer and use it in GitHub Desktop.
Save sharpred/ec2ed54e74411a0333a06bb0fe1436b1 to your computer and use it in GitHub Desktop.
simple monad
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