Skip to content

Instantly share code, notes, and snippets.

@tabatkins
Created August 30, 2017 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tabatkins/6f452e2b1d7903a7b3100d79e826ec8c to your computer and use it in GitHub Desktop.
Save tabatkins/6f452e2b1d7903a7b3100d79e826ec8c to your computer and use it in GitHub Desktop.
The IO monad, in JS
class IO {
constructor(val) {
this._fn = ()=>val;
return this;
}
static fromFn(fn) {
const ret = new IO();
ret._fn = fn;
return ret;
}
map(fn) {
return IO.fromFn(()=>fn(this.runIO()));
}
flatten() {
return IO.fromFn(()=>this.runIO().runIO());
}
runIO() {
return this._fn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment