UNPKG

@jsoldi/hkt

Version:

Higher kinded types for typescript and a few utility monads.

27 lines 959 B
import { functor } from "./functor.js"; import { maybe } from "../types/maybe.js"; import { pipe } from "../core/utils.js"; const is_unfold = Symbol('is_unfold'); /** Creates an `IUnfold` from an `IUnfoldBase`. */ export function unfold(base) { if (is_unfold in base) return base; return pipe(base, base => ({ ...functor(base), ...base }), base => { const iterate = f => base.unfold(a => base.scalar.unit(maybe.map(f(a), a2 => [a, a2]))); const forLoop = (init, pred, next) => iterate(a => pred(a) ? maybe.just(next(a)) : maybe.nothing)(init); const range = (start, end) => forLoop(start, t => t <= end, t => t + 1); const replicate = (n, a) => base.map(range(1, n), _ => a); return { ...{ [is_unfold]: true }, iterate, forLoop, range, replicate, ...base, }; }); } //# sourceMappingURL=unfold.js.map