/** * Element type of any sync or async iterable. */ type IterableItem = I extends AsyncIterable ? T : I extends Iterable ? T : never; /** * Reduce return type: * - For an `AsyncIterable` input, always `Promise`. * - For a sync `Iterable`, `R` directly (which is itself a `Promise` when the reducer is async). */ type ReduceResult = I extends AsyncIterable ? Promise> : R; /** * Reduces values of an iterable (Array, Set, Iterable, AsyncIterable) into a single value using the provided reducer function. * * Works with: * * Synchronous iterables (`Array`, `Set`, custom `Iterable`) * * Asynchronous iterables (`AsyncIterable` / async generators) * * Synchronous or asynchronous reducer functions (if the reducer returns a promise the result becomes a promise) * * If the initial value isn't provided, the first emitted value is used as the accumulator (same semantics as `Array.prototype.reduce`). * When used with an empty iterable without an initial value an error is thrown. * * @example * ```ts * reduce([1,2,3], (a,b) => a + b, 0) // 6 * reduce(new Set(['a','b']), (a,b) => a + b) // 'ab' * await reduce(asyncGen(), (a,b) => a + b) // 6 * ``` * @group Iterable */ export declare function reduce | AsyncIterable, R>(iterable: I, reducer: (acc: IterableItem, value: IterableItem, index: number) => R): ReduceResult; export declare function reduce | AsyncIterable, R>(iterable: I, reducer: (acc: Awaited, value: IterableItem, index: number) => R, initial: Awaited): ReduceResult; /** * Maps each element of an iterable structure preserving the original container type. * * * `Array` → `Array` * * `Set` → `Set` * * `Map` → `Map` (mapper receives and returns `[key, value]`) * * `Record` → `Record` (mapper receives and returns `[key, value]`) * * `Iterable` → lazy `Iterable` (eagerly resolved when mapper is async) * * `AsyncIterable` → lazy `AsyncIterable` (mapper may be async) * * When the mapper returns a `Promise`, the function returns a `Promise` of the resulting container. * * @example * ```ts * map([1, 2, 3], v => v * 2) // [2, 4, 6] * await map([1, 2, 3], async v => v*2) // [2, 4, 6] (Promise) * map(new Set([1, 2]), v => v + 1) // Set { 2, 3 } * ``` * @group Iterable */ export declare function map(input: V[], mapper: (value: V, index: number) => Promise): Promise; export declare function map(input: V[], mapper: (value: V, index: number) => R): R[]; export declare function map(input: Map, mapper: (value: [K, V], index: number) => Promise<[K2, V2]>): Promise>; export declare function map(input: Map, mapper: (value: [K, V], index: number) => [K2, V2]): Map; export declare function map(input: Set, mapper: (value: T, index: number) => Promise): Promise>; export declare function map(input: Set, mapper: (value: T, index: number) => R): Set; export declare function map(input: AsyncIterable, mapper: (value: T, index: number) => R | Promise): AsyncIterable; export declare function map(input: Iterable, mapper: (value: T, index: number) => Promise): Promise>; export declare function map(input: Iterable, mapper: (value: T, index: number) => R): Iterable; export declare function map(input: Record, mapper: (value: [K, V], index: number) => [K2, V2]): Record; export declare function map(input: Record, mapper: (value: [K, V], index: number) => Promise<[K2, V2]>): Promise>; /** * Maps each element to an iterable, then flattens one level — preserving the original container type. * * When the mapper returns a `Promise`, the function returns a `Promise` of the resulting container. * * @example * ```ts * flatMap([1, 2, 3], v => [v, v]) // [1, 1, 2, 2, 3, 3] * await flatMap([1, 2], async v => [v, v + 10]) // [1, 11, 2, 12] * ``` * @group Iterable */ export declare function flatMap(input: V[], mapper: (value: V, index: number) => Promise>): Promise; export declare function flatMap(input: V[], mapper: (value: V, index: number) => Iterable): R[]; export declare function flatMap(input: Map, mapper: (value: [K, V], index: number) => Promise>): Promise>; export declare function flatMap(input: Map, mapper: (value: [K, V], index: number) => Iterable<[K2, V2]>): Map; export declare function flatMap(input: Set, mapper: (value: T, index: number) => Promise>): Promise>; export declare function flatMap(input: Set, mapper: (value: T, index: number) => Iterable): Set; export declare function flatMap(input: AsyncIterable, mapper: (value: T, index: number) => Iterable | AsyncIterable | Promise | AsyncIterable>): AsyncIterable; export declare function flatMap(input: Iterable, mapper: (value: T, index: number) => Promise>): Promise>; export declare function flatMap(input: Iterable, mapper: (value: T, index: number) => Iterable): Iterable; export declare function flatMap(input: Record, mapper: (value: [K, V], index: number) => Iterable<[K2, V2]>): Record; export declare function flatMap(input: Record, mapper: (value: [K, V], index: number) => Promise>): Promise>; /** * Filters an iterable structure preserving the original container type. * * When the predicate returns a `Promise`, the function returns a `Promise` of the resulting container. * * @example * ```ts * filter([1, 2, 3, 4], v => v % 2 === 0) // [2, 4] * await filter([1, 2, 3], async v => v > 1) // [2, 3] * ``` * @group Iterable */ export declare function filter(input: V[], predicate: (value: V, index: number) => Promise): Promise; export declare function filter(input: V[], predicate: (value: V, index: number) => value is R): R[]; export declare function filter(input: V[], predicate: (value: V, index: number) => boolean): V[]; export declare function filter(input: Map, predicate: (value: [K, V], index: number) => Promise): Promise>; export declare function filter(input: Map, predicate: (value: [K, V], index: number) => boolean): Map; export declare function filter(input: Set, predicate: (value: T, index: number) => Promise): Promise>; export declare function filter(input: Set, predicate: (value: T, index: number) => value is R): Set; export declare function filter(input: Set, predicate: (value: T, index: number) => boolean): Set; export declare function filter(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): AsyncIterable; export declare function filter(input: Iterable, predicate: (value: T, index: number) => Promise): Promise>; export declare function filter(input: Iterable, predicate: (value: T, index: number) => boolean): Iterable; export declare function filter(input: Record, predicate: (value: [K, V], index: number) => Promise): Promise>; export declare function filter(input: Record, predicate: (value: [K, V], index: number) => boolean): Record; /** * Iterates each element of an iterable structure, calling `fn` for side effects. * * Returns `Promise` for `AsyncIterable` inputs and when `fn` returns a `Promise`, * `void` otherwise. Sequential ordering is preserved with async callbacks. * * @group Iterable */ export declare function forEach(input: V[], fn: (value: V, index: number) => Promise): Promise; export declare function forEach(input: V[], fn: (value: V, index: number) => void): void; export declare function forEach(input: Map, fn: (value: [K, V], index: number) => Promise): Promise; export declare function forEach(input: Map, fn: (value: [K, V], index: number) => void): void; export declare function forEach(input: Set, fn: (value: T, index: number) => Promise): Promise; export declare function forEach(input: Set, fn: (value: T, index: number) => void): void; export declare function forEach(input: AsyncIterable, fn: (value: T, index: number) => void | Promise): Promise; export declare function forEach(input: Iterable, fn: (value: T, index: number) => Promise): Promise; export declare function forEach(input: Iterable, fn: (value: T, index: number) => void): void; export declare function forEach(input: Record, fn: (value: [K, V], index: number) => Promise): Promise; export declare function forEach(input: Record, fn: (value: [K, V], index: number) => void): void; /** * Returns the first element matching the predicate, or `undefined` if none match. * * Returns `Promise` when input is an `AsyncIterable` or the predicate returns a `Promise`. * Sequential evaluation with short-circuit when the predicate is async. * * @group Iterable */ export declare function find(input: V[], predicate: (value: V, index: number) => Promise): Promise; export declare function find(input: V[], predicate: (value: V, index: number) => value is R): R | undefined; export declare function find(input: V[], predicate: (value: V, index: number) => boolean): V | undefined; export declare function find(input: Map, predicate: (value: [K, V], index: number) => Promise): Promise<[K, V] | undefined>; export declare function find(input: Map, predicate: (value: [K, V], index: number) => boolean): [K, V] | undefined; export declare function find(input: Set, predicate: (value: T, index: number) => Promise): Promise; export declare function find(input: Set, predicate: (value: T, index: number) => value is R): R | undefined; export declare function find(input: Set, predicate: (value: T, index: number) => boolean): T | undefined; export declare function find(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): Promise; export declare function find(input: Iterable, predicate: (value: T, index: number) => Promise): Promise; export declare function find(input: Iterable, predicate: (value: T, index: number) => boolean): T | undefined; export declare function find(input: Record, predicate: (value: [K, V], index: number) => Promise): Promise<[K, V] | undefined>; export declare function find(input: Record, predicate: (value: [K, V], index: number) => boolean): [K, V] | undefined; /** * Returns `true` if at least one element matches the predicate. * * Returns a `Promise` when input is an `AsyncIterable` or the predicate returns a `Promise`. * * @group Iterable */ export declare function some(input: Map, predicate: (value: [K, V], index: number) => Promise): Promise; export declare function some(input: Map, predicate: (value: [K, V], index: number) => boolean): boolean; export declare function some(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): Promise; export declare function some(input: V[] | Set | Iterable, predicate: (value: V, index: number) => Promise): Promise; export declare function some(input: V[] | Set | Iterable, predicate: (value: V, index: number) => boolean): boolean; export declare function some(input: Record, predicate: (value: [K, V], index: number) => Promise): Promise; export declare function some(input: Record, predicate: (value: [K, V], index: number) => boolean): boolean; /** * Returns `true` if every element matches the predicate (vacuously true for empty inputs). * * Returns a `Promise` when input is an `AsyncIterable` or the predicate returns a `Promise`. * * @group Iterable */ export declare function every(input: Map, predicate: (value: [K, V], index: number) => Promise): Promise; export declare function every(input: Map, predicate: (value: [K, V], index: number) => boolean): boolean; export declare function every(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): Promise; export declare function every(input: V[] | Set | Iterable, predicate: (value: V, index: number) => Promise): Promise; export declare function every(input: V[] | Set | Iterable, predicate: (value: V, index: number) => boolean): boolean; export declare function every(input: Record, predicate: (value: [K, V], index: number) => Promise): Promise; export declare function every(input: Record, predicate: (value: [K, V], index: number) => boolean): boolean; /** * Returns the number of elements in any iterable structure. * * For `AsyncIterable` returns `Promise` (full traversal). * For `Array`, `Set`, `Map`, `Record` uses native `length`/`size`/key count. * * @group Iterable */ export declare function size(input: T[] | Set | Map): number; export declare function size(input: AsyncIterable): Promise; export declare function size(input: Iterable): number; export declare function size(input: Record): number; /** * Collects all elements of an iterable structure into a fresh array. * * For `Map` and `Record` produces an array of `[key, value]` entries. * For `AsyncIterable` returns a `Promise`. * * @group Iterable */ export declare function toArray(input: V[] | Set): V[]; export declare function toArray(input: Map): [K, V][]; export declare function toArray(input: AsyncIterable): Promise; export declare function toArray(input: Iterable): T[]; export declare function toArray(input: Record): [K, V][]; /** * Returns a new container with the first `n` elements of the input, * preserving the original container type. * * For `AsyncIterable`/`Iterable` the result is lazy and stops the source iterator * after `n` elements. Negative `n` is treated as `0`. * * @example * ```ts * take([1, 2, 3, 4], 2) // [1, 2] * take(new Set([1, 2, 3]), 2) // Set { 1, 2 } * take({ a: 1, b: 2, c: 3 }, 2) // { a: 1, b: 2 } * ``` * @group Iterable */ export declare function take(input: V[], n: number): V[]; export declare function take(input: Map, n: number): Map; export declare function take(input: Set, n: number): Set; export declare function take(input: AsyncIterable, n: number): AsyncIterable; export declare function take(input: Iterable, n: number): Iterable; export declare function take(input: Record, n: number): Record; /** * Returns a new container skipping the first `n` elements of the input, * preserving the original container type. * * Negative `n` is treated as `0`. * * @example * ```ts * drop([1, 2, 3, 4], 2) // [3, 4] * drop(new Set([1, 2, 3]), 1) // Set { 2, 3 } * ``` * @group Iterable */ export declare function drop(input: V[], n: number): V[]; export declare function drop(input: Map, n: number): Map; export declare function drop(input: Set, n: number): Set; export declare function drop(input: AsyncIterable, n: number): AsyncIterable; export declare function drop(input: Iterable, n: number): Iterable; export declare function drop(input: Record, n: number): Partial>; /** * Takes leading elements as long as `predicate` returns truthy, stopping at the * first falsy result. Preserves the input container type. * * Async predicate or `AsyncIterable` input wraps the result in a `Promise` * (or `AsyncIterable` for `AsyncIterable` input). * * @example * ``` * takeWhile([1, 2, 3, 1], v => v < 3) // [1, 2] * takeWhile(new Set([1, 2, 3]), v => v < 3) // Set { 1, 2 } * ``` * @group Iterable */ export declare function takeWhile(input: V[], predicate: (value: V, index: number) => Promise): Promise; export declare function takeWhile(input: V[], predicate: (value: V, index: number) => boolean): V[]; export declare function takeWhile(input: Map, predicate: (value: [K, V], index: number) => boolean): Map; export declare function takeWhile(input: Set, predicate: (value: T, index: number) => boolean): Set; export declare function takeWhile(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): AsyncIterable; export declare function takeWhile(input: Iterable, predicate: (value: T, index: number) => boolean): Iterable; export declare function takeWhile(input: Record, predicate: (value: [K, V], index: number) => boolean): Partial>; /** * Drops leading elements as long as `predicate` returns truthy, then yields * the rest. Preserves the input container type. * * Async predicate or `AsyncIterable` input wraps the result in a `Promise` * (or `AsyncIterable` for `AsyncIterable` input). * * @example * ``` * dropWhile([1, 2, 3, 1], v => v < 3) // [3, 1] * dropWhile({ a: 1, b: 2, c: 3 }, ([, v]) => v < 2) * // { b: 2, c: 3 } * ``` * @group Iterable */ export declare function dropWhile(input: V[], predicate: (value: V, index: number) => Promise): Promise; export declare function dropWhile(input: V[], predicate: (value: V, index: number) => boolean): V[]; export declare function dropWhile(input: Map, predicate: (value: [K, V], index: number) => boolean): Map; export declare function dropWhile(input: Set, predicate: (value: T, index: number) => boolean): Set; export declare function dropWhile(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): AsyncIterable; export declare function dropWhile(input: Iterable, predicate: (value: T, index: number) => boolean): Iterable; export declare function dropWhile(input: Record, predicate: (value: [K, V], index: number) => boolean): Partial>; /** * Splits an iterable structure into two — `[matching, rest]` — by a predicate. * * Both halves preserve the input container type. With an async predicate the * result is wrapped in a `Promise`. * * @example * ```ts * partition([1, 2, 3, 4], v => v % 2 === 0) * // [[2, 4], [1, 3]] * * partition({ a: 1, b: 2 }, ([, v]) => v > 1) * // [{ b: 2 }, { a: 1 }] * ``` * @group Iterable */ export declare function partition(input: V[], predicate: (value: V, index: number) => Promise): Promise<[V[], V[]]>; export declare function partition(input: V[], predicate: (value: V, index: number) => boolean): [V[], V[]]; export declare function partition(input: Map, predicate: (value: [K, V], index: number) => Promise): Promise<[Map, Map]>; export declare function partition(input: Map, predicate: (value: [K, V], index: number) => boolean): [Map, Map]; export declare function partition(input: Set, predicate: (value: T, index: number) => Promise): Promise<[Set, Set]>; export declare function partition(input: Set, predicate: (value: T, index: number) => boolean): [Set, Set]; export declare function partition(input: AsyncIterable, predicate: (value: T, index: number) => boolean | Promise): Promise<[T[], T[]]>; export declare function partition(input: Iterable, predicate: (value: T, index: number) => Promise): Promise<[T[], T[]]>; export declare function partition(input: Iterable, predicate: (value: T, index: number) => boolean): [T[], T[]]; export declare function partition(input: Record, predicate: (value: [K, V], index: number) => Promise): Promise<[Record, Record]>; export declare function partition(input: Record, predicate: (value: [K, V], index: number) => boolean): [Record, Record]; /** * Combines multiple iterables element-wise into tuples, stopping at the shortest. * * If any input is an `AsyncIterable`, the result is a `Promise>`. * * @example * ```ts * zip([1, 2, 3], ['a', 'b', 'c']) * // [[1, 'a'], [2, 'b'], [3, 'c']] * * zip([1, 2, 3], ['a', 'b'], [true, false, true]) * // [[1, 'a', true], [2, 'b', false]] // shortest wins * ``` * @group Iterable */ export declare function zip | AsyncIterable>>(...inputs: T): T[number] extends Iterable ? Array<{ [K in keyof T]: IterableItem; }> : Promise; }>>; export {};