@jsoldi/hkt
Version:
Higher kinded types for typescript and a few utility monads.
36 lines • 1.12 kB
JavaScript
import { either } from "../either.js";
import { monad } from "../../classes/monad.js";
/** Creates a free monad from a functor. */
export function functorFree(freeBase) {
const unit = (a) => either.left(a);
const suspend = (f) => either.right(f);
const delay = (fa) => suspend(freeBase.map(fa, unit));
const foldFree = (pure, impure) => function go(ta) {
if (ta.right) {
return impure(freeBase.map(ta.value, go));
}
else {
return pure(ta.value);
}
};
const bind = (fa, f) => foldFree(f, either.right)(fa);
const mapFreeFrom = (transform) => function go(ft) {
if (ft.right) {
return either.right(freeBase.map(transform(ft.value), go));
}
else {
return unit(ft.value);
}
};
const mapFreeTo = (transform) => foldFree(either.left, ftag => either.right(transform(ftag)));
return {
...monad({ unit, bind }),
freeBase,
suspend,
delay,
foldFree,
mapFreeFrom,
mapFreeTo
};
}
//# sourceMappingURL=functorFree.js.map