/** * Flatmappable is a structure that allows a function that returns the concrete * structure to be applied to the value inside of the same type of concrete * structure. The resultant nested structure is then flattened. * * @module Flatmappable * @since 2.0.0 */ import "./_dnt.polyfills.js"; import type { $, Hold, Kind } from "./kind.js"; import type { Applicable } from "./applicable.js"; /** * A Flatmappable structure. */ export interface Flatmappable extends Applicable, Hold { readonly flatmap: (fati: (a: A) => $) => (ta: $) => $; } /** * Create a tap function for a structure with instances of Wrappable and * Flatmappable. A tap function allows one to break out of the functional * codeflow. It is generally not advised to use tap for code flow but to * consider an escape hatch to do things like tracing or logging. * * @since 2.0.0 */ export declare function createTap({ wrap, flatmap }: Flatmappable): (fn: (value: A) => void) => (ua: $) => $; /** * Create a bind function for a structure with instances of Mappable and * Flatmappable. A bind function allows one to flatmap into named fields in a * struct, collecting values from the result of the flatmap in the order that * they are completed. * * @since 2.0.0 */ export declare function createBind({ flatmap, map }: Flatmappable): (name: Exclude, faui: (a: A) => $) => (ua: $) => $; /** * Derive a Flatmappable instance from unwrap, flatmap, and a Kind. * This is the simplest way to get a Flatmappable instance. * * @example * ```ts * import type { Kind, Out } from "./kind.ts"; * import { createFlatmappable } from "./flatmappable.ts"; * import { pipe } from "./fn.ts"; * * // Create a Kind for Promise * interface KindPromise extends Kind { * readonly kind: Promise>; * }; * * // Create an of and chain function for Promise * const wrap = (a: A): Promise => Promise.resolve(a); * const flatmap = (faui: (a: A) => Promise) => * (ua: Promise): Promise => ua.then(faui); * * // Derive a Flatmappable for Promise * const M = createFlatmappable({ wrap, flatmap }); * * const result = await pipe( * M.wrap((n: number) => (m: number) => n + m), * M.apply(M.wrap(1)), * M.apply(M.wrap(1)), * ); // 2 * ``` * * @experimental * * @since 2.0.0 */ export declare function createFlatmappable({ wrap, flatmap }: Pick, "wrap" | "flatmap">): Flatmappable;