@jsoldi/hkt
Version:
Higher kinded types for typescript and a few utility monads.
41 lines • 1.89 kB
TypeScript
import { ITypeClass, $, $K1, $B2 } from "../core/hkt.js";
import { TypeClassArg } from "./utilities.js";
import { IMonad } from "./monad.js";
/** The minimal definition of a semigroup. */
export interface ISemigroup<F> extends ITypeClass<F> {
/** Combines two values using an associative operation. */
append: <A>(fa: $<F, A>, fb: $<F, A>) => $<F, A>;
}
/** The minimal definition of a monoid. */
export interface IMonoidBase<F> extends ISemigroup<F> {
/** Provides the identity element. */
empty: <A>() => $<F, A>;
}
/** The monoid interface, providing functions for working with monoids. */
export interface IMonoid<F> extends IMonoidBase<F> {
/** Combines two values using an associative operation. */
mappend<A>(fa: $<F, A>): (fb: $<F, A>) => $<F, A>;
/** Combines a list of values. */
concat<A>(fas: $<F, A>[]): $<F, A>;
/** Returns the value if the condition is true, otherwise returns the identity element. */
when(b: unknown): <A>(fa: $<F, A>) => $<F, A>;
/** Combines a list of values with a separator. */
join<A>(separator: $<F, A>): (fas: $<F, A>[]) => $<F, A>;
/** Returns the dual monoid with the operation reversed. */
dual(): IMonoid<F>;
/** Lifts this monoid into a monad. */
liftMonoid<M>(m: IMonad<M>): IMonoid<$B2<M, F>>;
}
declare const is_monoid: unique symbol;
export type MonoidArg<F> = TypeClassArg<IMonoidBase<F>, IMonoid<F>, typeof is_monoid>;
/** The monoid factory. */
export interface IMonoidFactory {
/** Creates an `IMonoid` from an `IMonoidBase`. */
<F>(base: MonoidArg<F>): IMonoid<F>;
/** Creates an `IMonoid` with a fixed type argument. */
concrete<T>(empty: T, append: (a: T, b: T) => T): IMonoid<$K1<T>>;
}
/** The monoid factory, providing functions for working with monoids. */
export declare const monoid: IMonoidFactory;
export {};
//# sourceMappingURL=monoid.d.ts.map