Skip to content

Instantly share code, notes, and snippets.

@MarkusPfundstein
Last active September 16, 2016 09:29
Show Gist options
  • Save MarkusPfundstein/3ee508f2c8de6d6007425ecf3b572df2 to your computer and use it in GitHub Desktop.
Save MarkusPfundstein/3ee508f2c8de6d6007425ecf3b572df2 to your computer and use it in GitHub Desktop.
eventually correct implementation of IO Monad in JS
class IO extends Monad {
constructor(fn) {
super();
this.__value = fn;
}
static of(fn) {
const io = new IO(() => fn);
Object.freeze(io);
return io;
}
map(f) {
return new IO(compose(this.__value, f));
}
mapP(res, rej) {
return this.map(then(res, rej));
}
flatMap(f) {
/*
return new IO(() => {
const r = this.map(f).performIO().performIO();
return r;
});
*/
// same result as above. So what would be better?
return this.map(f).performIO();
}
join() {
return this.__value;
}
performIO() {
return this.__value();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment