purify-ts-io
Version:
IO monad compatible with purify-ts
33 lines • 917 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class IO {
constructor(effect) {
this.effect = effect;
}
// Run the deferred effect
run() {
return this.effect();
}
// Functor map: applies a function to the result of the IO effect
map(fn) {
return new IO(() => fn(this.run()));
}
// Apply (ap): applies a function within an IO context to another IO value
ap(fab) {
return fab.chain(fn => this.map(fn));
}
// Chain (flatMap): applies a function that returns an IO, creating a new IO
chain(fn) {
return new IO(() => fn(this.run()).run());
}
// Lift a pure value into an IO context
static of(a) {
return new IO(() => a);
}
// Lift a function into an IO effect
static from(fn) {
return new IO(fn);
}
}
exports.default = IO;
//# sourceMappingURL=index.js.map