/** * Higher order functions for iterators. The async versions are in * `@hyperjump/pact/async`. */ import type { ZipOptions, ZipLongestOptions, ZipPadding, ZipKey } from "./common.d.ts"; export { pipe, compose, range } from "./common.js"; export type { ZipOptions, ZipLongestOptions, ZipPadding } from "./common.d.ts"; /** * Apply a function to every value in the iterator. Like the native Iterator * helpers, functions that take a callback pass it each value and its index. */ export const map: (fn: Mapper) => (iterable: Iterable) => IteratorObject; export type Mapper = (item: A, index: number) => B; /** * Apply a function to every value in the iterator, but yield the original * value, not the result of the function. */ export const tap: (fn: Tapper) => (iterable: Iterable) => IteratorObject; export type Tapper = (item: A, index: number) => void; /** * Yields only the values in the iterator that pass the predicate function. * A predicate that's a type guard narrows the type of the values. */ export const filter: ( (fn: TypeGuard) => (iterable: Iterable) => IteratorObject ) & ( (fn: Predicate) => (iterable: Iterable) => IteratorObject ); export type Predicate = (item: A, index: number) => unknown; export type TypeGuard = (item: A, index: number) => item is S; /** * Same as `reduce` except it emits the accumulated value after each update */ export const scan: (fn: Reducer, initial: B) => (iterable: Iterable) => IteratorObject; /** * Yields values from the iterator with all sub-iterator elements concatenated * into it recursively up to the specified depth. The default depth is 1. */ export const flatten: (depth?: Depth) => (iterable: Iterable) => IteratorObject, undefined, unknown>; export type NestedIterable = Iterable>; /** * What `flatten` yields for a value of type `A` that's `Depth` levels from * the last one it flattens. A string isn't flattened. A `Depth` that isn't * a literal number, like `Infinity`, flattens the type 20 levels. */ export type Flattened = { done: A; recur: A extends string ? A : A extends Iterable ? Flattened : A; }[Depth extends 0 ? "done" : `${Depth}` extends `-${string}` ? "done" : "recur"]; /** * Apply a function that returns an iterable to every value in the iterator, * and yield the values of each iterable. Unlike `flatten`, it goes one level * deep, and `fn` has to return an iterable or an iterator, not a string or * other value. */ export const flatMap: >(fn: Mapper) => (iterable: Iterable) => IteratorObject, undefined, unknown>; export type FlatMapper = Mapper>; /** * An iterable or an iterator of `A` that isn't a string, which is what * `flatMap`'s callback returns and what `zip` takes. */ export type Flattenable = (Iterable | Iterator) & object; export type FlattenableItem = R extends Iterable ? A : R extends Iterator ? A : never; /** * Yields all the values in the iterator except for the first `count` * values. `count` is truncated to an integer. One that's NaN or negative throws a * RangeError. */ export const drop: (count: number) => (iterable: Iterable) => IteratorObject; /** * Same as `drop` but instead of dropping a specific number of values, it drops * values while `fn` returns a truthy value and then yields the remaining values. */ export const dropWhile: (fn: Predicate) => (iterable: Iterable) => IteratorObject; /** * Yields the first `count` values in the iterator. `count` is truncated to an integer. One that's NaN or negative throws a * RangeError. */ export const take: (count: number) => (iterable: Iterable) => IteratorObject; /** * Same as `take` but instead of yielding a specific number of values, it yields * values while `fn` returns a truthy value and drops the rest. */ export const takeWhile: (fn: Predicate) => (iterable: Iterable) => IteratorObject; /** * Returns the first value in the iterator. The iterator is closed after * the first value is read, so the rest can't be read from it. */ export const head: (iterable: Iterable) => A | undefined; /** * Yields nothing. */ export const empty: () => IteratorObject; /** * Yields tuples with the next value of each iterable, like `Iterator.zip` from * the Joint Iteration proposal. A string isn't accepted as an iterable. * * - `mode: "shortest"` (the default) stops when any of them is done. * - `mode: "longest"` stops when all of them are done, and uses the value at * the same position in `padding` for the ones that are done, or * `undefined`. * - `mode: "strict"` throws a TypeError if they aren't all done at the same * time. * * When it stops, it closes the iterables that aren't done. */ export const zip: ( []>(iterables: readonly [...T], options?: ZipOptions) => IteratorObject<{ [K in keyof T]: FlattenableItem }, undefined, unknown> ) & ( [], const P extends Iterable = []>(iterables: readonly [...T], options: ZipOptions | ZipLongestOptions

) => IteratorObject<{ [K in keyof T]: FlattenableItem | ZipPadding }, undefined, unknown> ) & ( >(iterables: Iterable, options?: ZipOptions) => IteratorObject[], undefined, unknown> ) & ( , const P extends Iterable = []>(iterables: Iterable, options: ZipOptions | ZipLongestOptions

) => IteratorObject<(FlattenableItem | ZipPadding)[], undefined, unknown> ); /** * Same as `zip`, but takes an object of iterables, and yields objects with the * same keys, like `Iterator.zipKeyed` from the Joint Iteration proposal. Keys * whose value is `undefined` are left out. In `longest` mode, `padding` is an * object with a value for each key. */ export const zipKeyed: ( (iterables: T, options?: ZipOptions) => IteratorObject<{ [K in keyof T as ZipKey]: ZipKeyedItem }, undefined, unknown> ) & ( (iterables: T, options: ZipOptions | ZipLongestOptions

) => IteratorObject<{ [K in keyof T as ZipKey]: ZipKeyedItem | (K extends keyof P ? P[K] : undefined) }, undefined, unknown> ); export type ZipKeyedInputs = Record | undefined>; type ZipKeyedItem = FlattenableItem | (undefined extends R ? undefined : never); /** * Yields values from each iterator in order. */ export const concat: []>(...iterables: T) => IteratorObject, undefined, unknown>; /** * Reduce an iterator to a single value. Without an initial value, the first * value in the iterator is the initial value, and an empty iterator throws a * TypeError. */ export const reduce: ( (fn: Reducer, initial: B) => (iterable: Iterable) => B ) & ( (fn: Reducer) => (iterable: Iterable) => A ); export type Reducer = (acc: B, item: A, index: number) => B; /** * Returns a boolean indicating whether or not all values in the iterator passes * the predicate function. */ export const every: (fn: Predicate) => (iterable: Iterable) => boolean; /** * Returns a boolean indicating whether or not there exists a value in the * iterator that passes the predicate function. */ export const some: (fn: Predicate) => (iterable: Iterable) => boolean; /** * Returns the first value that passes the predicate function. A predicate * that's a type guard narrows the type of the value. */ export const find: ( (fn: TypeGuard) => (iterable: Iterable) => S | undefined ) & ( (fn: Predicate) => (iterable: Iterable) => B | undefined ); /** * Call a function with every value in the iterator. Unlike `tap`, it ends a * pipeline. */ export const forEach: (fn: Tapper) => (iterable: Iterable) => void; /** * Returns the number of items in the iterator. */ export const count: (iterable: Iterable) => number; /** * Collect all the items in the iterator into an array. */ export const toArray: (iterable: Iterable) => A[]; /** * Collect all the items in the iterator into a Set. */ export const toSet: (iterable: Iterable) => Set; /** * Collect all the key/value tuples in the iterator into a Map. */ export const toMap: (iterable: Iterable) => Map; /** * Collect all the key/value tuples in the iterator into an Object. */ export const toObject: (iterable: Iterable) => Record; /** * Collect all the items in the iterator into a string separated by the * separator token. */ export const join: (separator: string) => (iterable: Iterable) => string;