/** * The State module contains the State structure. The purpose of State is to * have modifiable date in an immutable workflow. This structure must preserve * purity, so that subsequent executions of a state workflow with the same * initial conditions produce the exact same results. * * @module State * @since 2.0.0 */ import "./_dnt.polyfills.js"; import type { InOut, Kind, Out } from "./kind.js"; import type { Applicable } from "./applicable.js"; import type { Combinable } from "./combinable.js"; import type { Flatmappable } from "./flatmappable.js"; import type { Initializable } from "./initializable.js"; import type { Mappable } from "./mappable.js"; import type { Wrappable } from "./wrappable.js"; /** * The State type represents the core State structure. The input/output * variable E is invariant, and the output variable A is covariant. * * @since 2.0.0 */ export type State = (e: E) => [A, E]; /** * Specifies State as a Higher Kinded Type, with covariant parameter A in the * 0th index of any substitutions and invariant parameter E in the 0th parameter * of any substitutions. * * @since 2.0.0 */ export interface KindState extends Kind { readonly kind: State, Out>; } /** * Construct a trivial State from values E and A. * * @example * ```ts * import * as S from "./state.ts"; * * const state = S.state(1, 2); * * const result = state(10); // [2, 1] * ``` * * @since 2.0.0 */ export declare function state(a: A, e: E): State; /** * An instance of Id that makes the State structure a Category. This is often * used at the beginning of a State workflow to specify the type of data within * a State structure. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const num = pipe( * S.id(), * S.map(n => n + 1), * ); * * const result = num(1); // [2, 1] * ``` * * @since 2.0.0 */ export declare function id(): State; /** * Construct a State from a function E => A. * * @example * ```ts * import * as S from "./state.ts"; * * const length = S.gets((s: string) => s.length); * * const result = length("Hello World"); // [11, "Hello World"] * ``` * * @since 2.0.0 */ export declare function gets(fea: (e: E) => A): State; /** * Construct a State from a static state value E. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const state = pipe( * S.id(), * S.flatmap(n => pipe(S.id(), S.map(m => m + n))), * ); * * const result = state(100); // [2, 1] * ``` * * @since 2.0.0 */ export declare function put(e: E): State; /** * Construct a State from a function E => E. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const state = S.modify((n: number) => n + 100); * * const result = state(1); // [undefined, 101] * ``` * * @since 2.0.0 */ export declare function modify(fee: (e: E) => E): State; /** * Construct a State from a static value A. * * @example * ```ts * import * as S from "./state.ts"; * * const state = S.wrap(1); * * const result = state(null); // [1, null] * ``` * * @since 2.0.0 */ export declare function wrap(a: A): State; /** * Map over the covariant value A in State. * * @example * ```ts * import * as S from "./state.ts"; * import * as A from "./array.ts"; * import { pipe } from "./fn.ts"; * * const work = pipe( * S.id(), * S.map(n => A.range(n)), * ); * * const result1 = work(1); // [[0], 1] * const result2 = work(3); // [[0, 1, 2], 3] * ``` * * @since 2.0.0 */ export declare function map(fai: (a: A) => I): (ta: State) => State; /** * Apply the A value of State to the (a: A) => I value of * State I>, producing a State. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const work = pipe( * S.id(), * S.map(s => (n: number) => s.repeat(n)), * S.apply(S.gets(s => s.length)) * ); * * const result1 = work("Hi"); // ["HiHi", "Hi"] * const result2 = work("Hello"); * // ["HelloHelloHelloHelloHello", "Hello"] * ``` * * @since 2.0.0 */ export declare function apply(ua: State): (ufai: State I>) => State; /** * Pass the A value in a State into a function (a: A) => State. This * results in a new State. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const state = pipe( * S.id(), * S.flatmap(n => S.wrap(n + 1)), * ); * * const result1 = state(1); // [2, 1] * const result2 = state(2); // [3, 2] * ``` * * @since 2.0.0 */ export declare function flatmap(fati: (a: A) => State): (ta: State) => State; /** * Extract the result value A by executing State with an S value. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * S.id(), * S.map(n => n + 1), * S.evaluate(10), * ); // 11 * ``` * * @since 2.0.0 */ export declare function evaluate(s: S): (ta: State) => A; /** * Extract the ending state value S by executing State with an S value. * * @example * ```ts * import * as S from "./state.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * S.id(), * S.map(n => n + 1), * S.execute(10), * ); // 10 * ``` * * @since 2.0.0 */ export declare function execute(s: S): (ta: State) => S; /** * @since 2.0.0 */ export declare function getCombinableState(CE: Combinable, CA: Combinable): Combinable>; /** * @since 2.0.0 */ export declare function getInitializableState(IE: Initializable, IA: Initializable): Initializable>; /** * @since 2.0.0 */ export declare const ApplicableState: Applicable; /** * The canonical implementation of Flatmappable for State. It contains * the methods wrap, apply, map, join, and flatmap. * * @since 2.0.0 */ export declare const FlatmappableState: Flatmappable; /** * @since 2.0.0 */ export declare const MappableState: Mappable; /** * @since 2.0.0 */ export declare const WrappableState: Wrappable; /** * @since 2.0.0 */ export declare const tap: (fn: (value: A) => void) => (ua: State) => State; /** * @since 2.0.0 */ export declare const bind: (name: Exclude, faui: (a: A) => State) => (ua: State) => State; /** * @since 2.0.0 */ export declare const bindTo: (name: N) => (ua: State) => State;