mcard-js
Version:
MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers
65 lines (63 loc) • 1.16 kB
JavaScript
// src/monads/IO.ts
var IO = class _IO {
constructor(effect) {
this.effect = effect;
}
/**
* Create an IO from an effect (lazy evaluation)
*/
static of(effect) {
return new _IO(effect);
}
/**
* Lift a pure value into IO
*/
static pure(value) {
return new _IO(() => value);
}
/**
* Monadic bind - chain IO operations
*/
bind(fn) {
return new _IO(async () => {
const result = await this.run();
return fn(result).run();
});
}
/**
* Map a function over the result
*/
map(fn) {
return new _IO(async () => {
const result = await this.run();
return fn(result);
});
}
/**
* Execute the IO and get the result
*/
async run() {
return this.effect();
}
/**
* Run multiple IOs in sequence
*/
static sequence(ios) {
return new _IO(async () => {
const results = [];
for (const io of ios) {
results.push(await io.run());
}
return results;
});
}
/**
* Run multiple IOs in parallel
*/
static parallel(ios) {
return new _IO(() => Promise.all(ios.map((io) => io.run())));
}
};
export {
IO
};