import type { Comparator, Fn, Fn0, IObjectOf } from "@thi.ng/api"; import type { Reduced } from "./reduced.js"; export type Transducer = (rfn: Reducer) => Reducer; /** * A transducer or a custom type with a {@link IXform} implementation. */ export type TxLike = Transducer | IXform; /** * Custom version of {@link TxLike} for use with {@link multiplex} and * {@link multiplexObj}. */ export type MultiplexTxLike = TxLike | [TxLike, boolean]; /** * Function which combines a new value of type `A` with accumulator of type `B`. * If the reduction should terminate early, the function should wrap the result * via {@link reduced}. */ export type ReductionFn = (acc: B, x: A) => B | Reduced; /** * A 3-tuple of functions defining the different stages of a reduction process. * * @remarks * The items in order: * * 1. Initialization function used to produce an initial default result (only * used if no such initial result was given by the user) * 2. Completion function to post-process an already reduced result (for most * reducers this is merely the identity function). Also see {@link reducer}. * 3. Accumulation function, merging a new input value with the currently * existing (partially) reduced result. */ export type Reducer = [Fn0, Fn, ReductionFn]; export type MaybeReduced = Reduced | T; /** * Interface for types able to provide some internal functionality (or * derive some related transformation) as {@link Transducer}. * Implementations of this interface can be directly passed to all * functions in this package where a `Transducer` arg is expected. * * @example * ```ts tangle:../export/ixform.ts * import { * comp, drop, map, push, range, takeNth, transduce, * type IXform * } from "@thi.ng/transducers"; * * class Mul implements IXform { * constructor(public factor = 10) {} * * xform() { return map((x: number) => this.factor * x); } * } * * console.log( * transduce(new Mul(11), push(), range(4)) * ); * // [0, 11, 22, 33, 44] * * // also usable w/ comp() * const res = transduce( * comp( * drop(1), * new Mul(11), * takeNth(2) * ), * push(), * range(4) * ); * * console.log(res); * // [11, 33] * ``` */ export interface IXform { /** * Returns type specific operation as transducer. Internally called * by functions in this package which expect transducer args. Users * don't need to call this manually. */ xform(): Transducer; } export interface IReducible { /** * Used for optimized (rather than generic) iteration of a data structure * for reduction purposes. Supported by {@link reduce} and * {@link transduce}. * * @remarks * Example implementations: * * - [`SortedMap`](https://docs.thi.ng/umbrella/associative/classes/SortedMap.html) * - [`SortedSet`](https://docs.thi.ng/umbrella/associative/classes/SortedSet.html) * - [`DCons`](https://docs.thi.ng/umbrella/dcons/classes/DCons.html) * - [`DRing`](https://docs.thi.ng/umbrella/dcons/classes/DRing.html) * - [`SOL`](https://docs.thi.ng/umbrella/dcons/classes/SOL.html) * * @param rfn * @param acc */ $reduce(rfn: ReductionFn, acc: B | Reduced): B | Reduced; } export type TransformFn = (x: any) => any; export type TransformSubSpec = IObjectOf; export interface TransformSpec extends Array { [0]: TransformFn; [1]?: TransformSubSpec; } export interface SortOpts { /** * Sort key lookup function. * Default: `identity` */ key: Fn; /** * Comparator. * Default: `thi.ng/compare/compare` */ compare: Comparator; } export interface GroupByOpts { key: Fn; group: Reducer; } //# sourceMappingURL=api.d.ts.map