/** * This function returns an iterable where each item was produced by applying * `mapperFn` to an item of `iterable`. * * ```ts * assert.deepEqual( * toArray(Iterable.map(x => x + x, ['a', 'b', 'c'])), * ['aa', 'bb', 'cc'] * ); * ``` */ declare function map(mapperFn: (x: In) => Out, iterable: AsyncOrSyncIterable): AsyncIterable; /** * This function returns an iterable that contains only those items of `iterable` * for which `filterFn` returns a truthy value. * * ```ts * assert.deepEqual( * toArray(Iterable.filter(x => x < 0, [-1, 3, -4, 8])), * [-1, -4] * ); * ``` */ declare function filter(filterFn: (x: Item) => boolean, iterable: AsyncOrSyncIterable): AsyncIterable; /** * Each item of `iterable` is converted to zero or more items in the returned * iterable, depending on whether `mapperFn` returns a single value or an Array * (and on how long that Array is). This enables us to: * - Filter and map at the same time. We omit by returning `[]` and map by returning `[result]`. * - Expand single values into multiple values (see below). * * ```ts * assert.deepEqual( * toArray(Iterable.flatMap(x => x, ['a', 'b', 'c'])), * ['a', 'b', 'c'] * ); * assert.deepEqual( * toArray(Iterable.flatMap(x => [], ['a', 'b', 'c'])), * [] * ); * assert.deepEqual( * toArray(Iterable.flatMap(x => [x], ['a', 'b', 'c'])), * ['a', 'b', 'c'] * ); * assert.deepEqual( * toArray(Iterable.flatMap(x => [x, x], ['a', 'b', 'c'])), * ['a', 'a', 'b', 'b', 'c', 'c'] * ); * ``` */ declare function flatMap(mapperFn: (x: In) => Out | Array, iterable: AsyncOrSyncIterable): AsyncIterable; /** * Returns an iterable with the first `limit` items of `iterable`. * * ```ts * function* naturalNumbers() { * for(let i=0;; i++) { * yield i; * } * } * assert.deepEqual( * toArray(Iterable.take(3, naturalNumbers())), * [0, 1, 2] * ); * assert.deepEqual( * toArray(Iterable.take(2, ['a', 'b', 'c'])), * ['a', 'b'] * ); * ``` */ declare function take(limit: number, iterable: AsyncOrSyncIterable): AsyncIterable; /** * Returns an iterable with all items of `iterable`, except for the first `limit` ones. * * ```ts * assert.deepEqual( * toArray(Iterable.drop(1, ['a', 'b', 'c'])), * ['b', 'c'] * ); * ``` */ declare function drop(limit: number, iterable: AsyncOrSyncIterable): AsyncIterable; /** * Each item `x` of `iterable` is converted to an item `[index, x]` in the * returned iterable. * * While Array methods such as `.map()` and `.filter()` always provide indices, * the functions in this module don’t. Therefore, this function is useful * whenever we need indices – e.g., to determine which item is first in an * iterable. * * ```ts * assert.deepEqual( * toArray(Iterable.asIndexedPairs(['a', 'b', 'c'])), * [[0, 'a'], [1, 'b'], [2, 'c']] * ); * ``` */ declare function asIndexedPairs(iterable: AsyncOrSyncIterable): AsyncIterable<[number, Item]>; /** * Feeds all items of `iterable` to `reducer()` which folds them into `acc`. * The last `acc` returned by `reducer()` is the result of this function. * ```ts * assert.deepEqual( * Iterable.reduce((acc, item) => acc + item, ['a', 'b', 'c']), * 'abc' * ); * assert.deepEqual( * Iterable.reduce((acc, item) => acc + item, 'x', ['a', 'b', 'c']), * 'xabc' * ); * ``` * @param initialValue The first value of `acc`. If it is missing, the first item of `iterable` is used, instead. * @throws TypeError If `iterable` is empty and no `initialValue` is provided. */ declare function reduce(reducer: (acc: Out, item: In) => Out, initialValue: Out, iterable: AsyncOrSyncIterable): Promise; declare function reduce(reducer: (acc: InOut, item: InOut) => InOut, iterable: AsyncOrSyncIterable): Promise; /** * Retrieves each item of `iterable` and invokes `fn()` with it. * * ```ts * const result = []; * Iterable.forEach(x => result.push(x + x), ['a', 'b', 'c']) * assert.deepEqual( * result, * ['aa', 'bb', 'cc'] * ); * ``` */ declare function forEach(fn: (item: Item) => void, iterable: AsyncOrSyncIterable): Promise; /** * Iterates over each `item` of `iterable` and returns `true` as soon as * `pred(item)` returns a truthy value. It short-circuits in that case and * doesn’t visit the remaining items of `iterable`. That means, we can use this * function with iterables of infinite length. * * This function returns `false` if `pred()` never returns a truthy value * and `iterable` is of finite length. * * ```ts * function* naturalNumbers() { * for(let i=0;; i++) { * yield i; * } * } * assert.equal( * await AsyncIterable.some((item) => item > 0, fi([5, -3, 12])), * true * ); * assert.equal( * await AsyncIterable.some((item) => item < 0, fi([5, -3, 12])), * true * ); * assert.equal( * await AsyncIterable.some((item) => item < -3, fi([5, -3, 12])), * false * ); * * assert.equal( * await AsyncIterable.some((item) => item > 10, fi(naturalNumbers())), * true * ); * ``` */ declare function some(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable): Promise; /** * Iterates over each `item` of `iterable` and returns `false` as soon as * `pred(item)` returns a falsy value. It short-circuits in that case and * doesn’t visit the remaining items of `iterable`. That means, we can use this * function with iterables of infinite length. * * This function returns `true` if `pred()` never returns a falsy value * and `iterable` is of finite length. * * ```ts * function* naturalNumbers() { * for(let i=0;; i++) { * yield i; * } * } * assert.equal( * Iterable.every((item) => item > 0, [5, -3, 12]), * false * ); * assert.equal( * Iterable.every((item) => item < 0, [5, -3, 12]), * false * ); * assert.equal( * Iterable.every((item) => item >= -3, [5, -3, 12]), * true * ); * * assert.equal( * Iterable.every((item) => item <= 10, naturalNumbers()), * false * ); * ``` */ declare function every(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable): Promise; /** * Iterates over each `item` of `iterable` and returns `item` as soon as * `pred(item)` returns a truthy value. It short-circuits in that case and * doesn’t visit the remaining items of `iterable`. That means, we can use this * function with iterables of infinite length. * * This function returns `undefined` if `pred()` never returns a truthy value * and `iterable` is of finite length. * * ```ts * function* naturalNumbers() { * for(let i=0;; i++) { * yield i; * } * } * assert.equal( * Iterable.find((item) => item > 0, [5, -3, 12, -8]), * 5 * ); * assert.equal( * Iterable.find((item) => item < 0, [5, -3, 12, -8]), * -3 * ); * * assert.equal( * Iterable.find((item) => item > 20, naturalNumbers()), * 21 * ); * ``` */ declare function find(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable): Promise; /** * `T` must include `unknown` somewhere, so that the Array stays mixed (heterogeneous). The 1-ary tuple in the type union is also required to ensure tuple-ness. */ declare type MixedArray = Array | [Elem]; /** * `T` must include `unknown` somewhere, so that the object stays mixed (heterogeneous). */ declare type MixedObject = { [key: string]: T; }; /** * Depending on its argument, this function converts: * - An n-ary Array of iterables into an iterable of n-ary Arrays whose elements * are the items of those iterables. * - An object whose property values are iterables into an iterable of objects * whose property values are the items of those iterables. * * ```ts * assert.deepEqual( * toArray(Iterable.zip({first: ['a', 'b'], second: [0, 1, 2] })), * [ {first: 'a', second: 0}, {first: 'b', second: 1} ] * ); * assert.deepEqual( * toArray(Iterable.zip([ ['a', 'b'], [0, 1, 2] ])), * [ ['a', 0], ['b', 1] ] * ); * ``` */ declare function zip>>(iterables: MArr): AsyncIterable<{ [Key in keyof MArr]: UnwrapAsyncOrSyncIterable; }>; declare function zip>>(iterables: MObj): AsyncIterable<{ [Key in keyof MObj]: UnwrapAsyncOrSyncIterable; }>; declare function toArray(iterable: AsyncOrSyncIterable): Promise>; declare const Functions: { map: typeof map; filter: typeof filter; flatMap: typeof flatMap; take: typeof take; drop: typeof drop; asIndexedPairs: typeof asIndexedPairs; reduce: typeof reduce; forEach: typeof forEach; some: typeof some; every: typeof every; find: typeof find; zip: typeof zip; toArray: typeof toArray; fromIterable: typeof fromIterable; }; export { Functions as AsyncIterable }; declare type AsyncOrSyncIterable = AsyncIterable | Iterable; declare type UnwrapAsyncOrSyncIterable = I extends AsyncIterable ? Item : UnwrapIterable; declare function fromIterable(iterable: Iterable): AsyncIterable; declare type UnwrapIterable = I extends AsyncIterable ? Item : I extends Iterable ? Item : never; //# sourceMappingURL=async.d.ts.map