effect-ts-folds
Version:
Recursion schemes for effect-ts.
20 lines • 1.5 kB
JavaScript
import { extract, fix, unfix } from '#fix';
import { fanout, pairWithFirst, pairWithSecond, succeedBy, traverseCovariant, traverseSuspended, } from '#util';
import { Effect, flow, pipe, Tuple as TU } from 'effect';
import { hyloE } from '../refold/schemes.js';
export const cataE = F => φ => hyloE(F)(a => pipe(a, unfix, Effect.succeed), φ);
export const paraE = (F) => (φ) => (fixed) => pipe(fixed, cataE(F)((fa) => {
const [fixed, effect] = pipe(fa, fanout(flow(traverseCovariant(F).map(TU.getFirst), fix), φ));
return pipe(effect, Effect.map(pairWithFirst(fixed)));
}), Effect.map(TU.getSecond));
export const zygoE = (F) => (f, φ) => flow(cataE(F)((fab) => pipe(fab, traverseCovariant(F).map(TU.getSecond), φ, pipe(fab, f, pairWithFirst, (Effect.map)))), Effect.map(TU.getFirst));
export const histoE = (F) => (φ) => {
const run = (fixed) => pipe(fixed, (unfix), traverseSuspended(F)(run), Effect.flatMap(fa => pipe(fa, φ, Effect.map(pairWithSecond(fa)))));
return flow(run, Effect.map(extract));
};
export const cata = F => φ => flow(pipe(φ, succeedBy, cataE(F)), Effect.runSync);
export const para = F => φ => flow(pipe(φ, succeedBy, paraE(F)), Effect.runSync);
export const zygo = F => (f, φ) => flow(zygoE(F)(f, succeedBy(φ)), Effect.runSync);
export const histo = F => φ => flow(pipe(φ, succeedBy, histoE(F)), Effect.runSync);
export const algebraToCVAlgebra = (F) => (alg) => flow(traverseCovariant(F).map(extract), alg);
//# sourceMappingURL=schemes.js.map