UNPKG

@jsoldi/hkt

Version:

Higher kinded types for typescript and a few utility monads.

29 lines 1.57 kB
import { KRoot, $ } from "../../core/hkt.js"; import { Left, Right } from "../either.js"; import { IMonad } from "../../classes/monad.js"; import { IFunctor } from "../../classes/functor.js"; /** The free monad type. */ export type Free<A, F> = Left<A> | Right<$<F, Free<A, F>>>; /** The higher-kinded type of the free monad. */ export interface KFree<F> extends KRoot { readonly 0: unknown; readonly body: Free<this[0], F>; } /** The free monad interface based on a functor. */ export interface IFunctorFree<F> extends IMonad<KFree<F>> { /** The functor underlying the free monad. */ readonly freeBase: IFunctor<F>; /** Suspends a computation in the free monad. */ suspend<A>(f: $<F, Free<A, F>>): Free<A, F>; /** Suspends a computation by wrapping a value in the free monad. Inverse of `IMonadFree`'s `run`. */ delay<A>(f: $<F, A>): Free<A, F>; /** Changes the underlying functor from another functor. */ mapFreeFrom<G, A>(transform: (ft: $<G, Free<A, G>>) => $<F, Free<A, G>>): (ft: Free<A, G>) => Free<A, F>; /** Changes the underlying functor to another functor. */ mapFreeTo<G, A>(transform: (ft: $<F, Free<A, G>>) => $<G, Free<A, G>>): (ft: Free<A, F>) => Free<A, G>; /** Folds the free monad into a single value by mapping it through the given functions. */ foldFree<A, B>(pure: (a: A) => B, impure: (fb: $<F, B>) => B): (ft: Free<A, F>) => B; } /** Creates a free monad from a functor. */ export declare function functorFree<F>(freeBase: IFunctor<F>): IFunctorFree<F>; //# sourceMappingURL=functorFree.d.ts.map