Skip to content

Iterables Reference

Coverage License NPM Version Open Issues Size

🔁 Iterable and AsyncIterable utils.

Usage

By default, all utils are meant to be used with Iterable values, to use AsyncIterable values the functions have to be imported from @lou.codes/iterables/asynchronous.

📦 Node

Install @lou.codes/iterables as a dependency:

Terminal window
1
pnpm add @lou.codes/iterables
2
# or
3
npm install @lou.codes/iterables
4
# or
5
yarn add @lou.codes/iterables

Import it and use it:

1
import { iterableToArray, map } from "@lou.codes/iterables";
2
3
const mapDouble = map((value: number) => value * 2);
4
5
iterableToArray(mapDouble([1, 2, 3])); // [2, 4, 6]

🦕 Deno

Import @lou.codes/iterables using the npm: prefix, and use it directly:

1
import { iterableToArray, map } from "npm:@lou.codes/iterables";
2
3
const mapDouble = map((value: number) => value * 2);
4
5
iterableToArray(mapDouble([1, 2, 3])); // [2, 4, 6]

🌎 Browser

Import @lou.codes/iterables using esm.sh, and use it directly:

1
<script type="module">
2
import { iterableToArray, map } from "https://esm.sh/@lou.codes/iterables";
3
4
const mapDouble = map(value => value * 2);
5
6
iterableToArray(mapDouble([1, 2, 3])); // [2, 4, 6]
7
</script>

Asynchronous

Asynchronous Common

createIterableIterator()

1
function createIterableIterator<GeneratorFunction>(
2
generatorFunction: GeneratorFunction,
3
): GeneratorFunction extends () => IsomorphicIterator<Item> ?
4
Readonly<
5
GeneratorFunction<GeneratorFunction> extends (
6
(..._arguments: _Arguments) => AsyncIterator<Item, any, undefined>
7
) ?
8
AsyncIterableIterator<Item>
9
: IterableIterator<Item>
10
>
11
: never;

Takes a generator function and returns an iterable iterator or asynchronous iterable iterator object.

Type parameters
Type parameter
GeneratorFunction extends () => IsomorphicIterator
Parameters
ParameterTypeDescription
generatorFunctionGeneratorFunctionGenerator to be used every time [Symbol.iterator] is called.
Returns

GeneratorFunction extends () => IsomorphicIterator<Item> ? Readonly<GeneratorFunction<GeneratorFunction> extends (…_arguments: _Arguments) => AsyncIterator<Item, any, undefined> ? AsyncIterableIterator<Item> : IterableIterator<Item>> : never

Iterable iterator object.

Example
1
const identityGenerator = function* (value) {
2
yield value;
3
};
4
const iterableIterator = createIterableIterator(identityGenerator);
5
6
const fooIdentity = iterableIterator("foo");
7
8
for (const value of fooIdentity) {
9
console.log(value); // "foo"
10
}
11
12
// Same IterableIterator as above, return values again:
13
14
for (const value of fooIdentity) {
15
console.log(value); // "foo"
16
}

View source


forEach()

1
function forEach<Item>(
2
callback: Unary<Item, Awaitable<void>>,
3
): (iterable: IsomorphicIterable<Item>) => Promise<void>;

For each function for iterables and asynchronous iterables.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
callbackUnary<Item, Awaitable<void>>Function to be called for every item of the iterable.
Returns

Function

Curried function that expects an iterable to loop over and has callback set in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Promise<void>

Example
1
const logEach = forEach(console.log);
2
3
logEach([1, 2, 3]); // Logs 1, 2 and 3 separately

View source

Asynchronous Generators

append()

1
function append<TailItem>(
2
tailIterable: IsomorphicIterable<TailItem>,
3
): <InitialItem>(
4
initialIterable: IsomorphicIterable<InitialItem>,
5
) => Readonly<AsyncIterableIterator<Awaited<InitialItem> | Awaited<TailItem>>>;

Appends one iterable or asynchronous iterable to another.

Type parameters
Type parameter
TailItem
Parameters
ParameterTypeDescription
tailIterableIsomorphicIterable<TailItem>Iterable or asynchronous to be appended.
Returns

Function

Curried generator function with tailIterable set in context.

Type parameters
Type parameter
InitialItem
Parameters
ParameterType
initialIterableIsomorphicIterable<InitialItem>
Returns

Readonly<AsyncIterableIterator<Awaited<InitialItem> | Awaited<TailItem>>>

Example
1
const appendNumbers = append([0, 1, 2, 3, 4]);
2
3
appendNumbers(["foo", "bar"]); // ["foo", "bar", 0, 1, 2, 3, 4]
4
appendNumbers([]); // [0, 1, 2, 3, 4]

View source


drop()

1
function drop(
2
amount: Numeric,
3
): <Item>(
4
iterable: IsomorphicIterable<Item>,
5
) => Readonly<AsyncIterableIterator<Awaited<Item>>>;

Drop the specified amount of items from the given iterable or asynchronous iterable.

Parameters
ParameterTypeDescription
amountNumericAmount of items to drop.
Returns

Function

Curried function with amount in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Readonly<AsyncIterableIterator<Awaited<Item>>>

Example
1
const drop2 = drop(2);
2
drop2([1, 2, 3, 4, 5]); // [3, 4, 5]

View source


filter()

1
function filter<Item, Filtered>(
2
predicate: Single<Filtered> extends Single<never> ? Unary<Item, boolean>
3
: Predicate<Item, Filtered>,
4
): (
5
iterable: IsomorphicIterable<Item>,
6
) => Readonly<AsyncIterableIterator<Awaited<Filtered>>>;

Filters items in an iterable or asynchronous iterable against a predicate and returns items that evaluated to true.

Type parameters
Type parameterValue
Item-
Filterednever
Parameters
ParameterTypeDescription
predicateSingle<Filtered> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Filtered>Predicate function to evaluate each item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Readonly<AsyncIterableIterator<Awaited<Filtered>>>

Example
1
const filterEven = filter((number: number) => number % 2 === 0);
2
3
iterableToArray(filterEven([1, 2, 3, 4])); // [2, 4]
4
iterableToArray(filterEven([1, 3, 5, 7])); // []

View source


flat()

1
function flat<Iterable>(
2
iterable: Iterable,
3
): Iterable extends IsomorphicIterable<Item> ?
4
Readonly<
5
Item extends IsomorphicIterable<SubItem> ?
6
AsyncIterableIterator<SubItem>
7
: AsyncIterableIterator<Item>
8
>
9
: never;

Flattens one level of the given iterable or asynchronous iterable.

Type parameters
Type parameter
Iterable extends IsomorphicIterable
Parameters
ParameterTypeDescription
iterableIterableIterable to flatten.
Returns

Iterable extends IsomorphicIterable<Item> ? Readonly<Item extends IsomorphicIterable<SubItem> ? AsyncIterableIterator<SubItem> : AsyncIterableIterator<Item>> : never

Iterable with flatten items.

Example
1
flat([1, 2, [3, 4]]); // [1, 2, 3, 4]

View source


initial()

1
function initial<Iterable>(
2
iterable: Iterable,
3
): IsomorphicIterableIterator<IsomorphicIterableItem<Iterable>>;

Get all elements except the last one of an iterable or asynchronous iterable.

Type parameters
Type parameter
Iterable extends IsomorphicIterable
Parameters
ParameterTypeDescription
iterableIterableIterable to get the items from.
Returns

IsomorphicIterableIterator<IsomorphicIterableItem<Iterable>>

Iterable with all items except the last one.

Example
1
initial([1, 2, 3]); // [1, 2]

View source


intersperse()

1
function intersperse<Separator>(
2
separator: Separator,
3
): <Item>(
4
iterable: IsomorphicIterable<Item>,
5
) => IsomorphicIterableIterator<Item | Awaited<Separator> | Awaited<Item>>;

Add the given separator between each element of the given iterable or asynchronous iterable.

Type parameters
Type parameter
Separator
Parameters
ParameterTypeDescription
separatorSeparatorSeparator to add between each element.
Returns

Function

Curried function with separator in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

IsomorphicIterableIterator<Item | Awaited<Separator> | Awaited<Item>>

Example
1
const intersperseComma = intersperse(",");
2
intersperseComma([1, 2, 3]); // [1, ",", 2, ",", 3]

View source


map()

1
function map<Item, MappedItem>(
2
mapper: Unary<Item, MappedItem>,
3
): (
4
iterable: IsomorphicIterable<Item>,
5
) => Readonly<AsyncIterableIterator<Awaited<MappedItem>>>;

Map for iterables and asynchronous iterables.

Type parameters
Type parameter
Item
MappedItem
Parameters
ParameterTypeDescription
mapperUnary<Item, MappedItem>Mapper function.
Returns

Function

Generator function with mapper function set in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Readonly<AsyncIterableIterator<Awaited<MappedItem>>>

Example
1
const double = value => value * 2;
2
const mapDouble = map(double);
3
4
mapDouble([1, 2, 3]); // [2, 4, 6]

View source


objectToEntries()

1
function objectToEntries<Key, Value>(
2
input: Readonly<Record<Key, Value>>,
3
): Readonly<AsyncIterableIterator<Entry<Key, Value>>>;

Yields all entries of an object (including symbols).

Type parameters
Type parameter
Key extends PropertyKey
Value
Parameters
ParameterTypeDescription
inputReadonly<Record<Key, Value>>Object to get entries from.
Returns

Readonly<AsyncIterableIterator<Entry<Key, Value>>>

Iterable with entries of the given object (including symbols).

Example
1
const entries = objectEntries({ a: 1, b: 2 });
2
entries.next(); // { value: ["a", 1], done: false }
3
entries.next(); // { value: ["b", 2], done: false }
4
entries.next(); // { value: undefined, done: true }

View source


prepend()

1
function prepend<InitialItem>(
2
initialIterable: IsomorphicIterable<InitialItem>,
3
): <TailItem>(
4
tailIterable: IsomorphicIterable<TailItem>,
5
) => Readonly<AsyncIterableIterator<Awaited<InitialItem> | Awaited<TailItem>>>;

Prepends one iterable or asynchronous iterable to another.

Type parameters
Type parameter
InitialItem
Parameters
ParameterTypeDescription
initialIterableIsomorphicIterable<InitialItem>Iterable or asynchronous iterable to be appended.
Returns

Function

Curried generator function with initialIterable set in context.

Type parameters
Type parameter
TailItem
Parameters
ParameterType
tailIterableIsomorphicIterable<TailItem>
Returns

Readonly<AsyncIterableIterator<Awaited<InitialItem> | Awaited<TailItem>>>

Example
1
const prependNumbers = prepend([0, 1, 2, 3, 4]);
2
prependNumbers(["foo", "bar"]); // [0, 1, 2, 3, 4, "foo", "bar"]

View source


random()

1
function random(
2
seed: string | Numeric,
3
): (from: number) => (to: number) => Readonly<AsyncIterableIterator<number>>;

Deterministic pseudo-random number generator.

⚠️ IMPORTANT: This only works in secure contexts (HTTPS/Node).

Parameters
ParameterTypeDescription
seedstring | NumericSeed to be used to generate random numbers.
Returns

Function

Curried generator function with seed set in context.

Parameters
ParameterType
fromnumber
Returns

Function

Parameters
ParameterType
tonumber
Returns

Readonly<AsyncIterableIterator<number>>

Example
1
const seededRandom = random("some seed");
2
const random0To10 = seededRandom(0)(10);
3
4
[...pick(2)(random0To10)]; // Two "random" values between 0 and 10
5
[...pick(2)(random0To10)]; // Same two "random" values between 0 and 10
See

View source


range()

1
function range<Step>(
2
step: Step,
3
): (
4
from: Step extends bigint ? bigint : number,
5
) => (
6
to: Step extends bigint ? bigint : number,
7
) => Readonly<AsyncIterableIterator<Step extends bigint ? bigint : number>>;

Range iterable generator (from from to to).

Type parameters
Type parameter
Step extends Numeric
Parameters
ParameterTypeDescription
stepStepStep size.
Returns

Function

Curried function with step set in context.

Parameters
ParameterType
fromStep extends bigint ? bigint : number
Returns

Function

Parameters
ParameterType
toStep extends bigint ? bigint : number
Returns

Readonly<AsyncIterableIterator<Step extends bigint ? bigint : number>>

Example
1
[...range(1)(0)(5)]; // [0, 1, 2, 3, 4, 5]
2
[...range(1)(5)(0)]; // [5, 4, 3, 2, 1, 0]

View source


repeat()

1
function repeat(
2
times: Numeric,
3
): <Item>(item: Item) => Readonly<AsyncIterableIterator<Awaited<Item>>>;

Repeat given item the specified amount of times (can be BigInt or Infinity times) as an iterable.

Parameters
ParameterType
timesNumeric
Returns

Function

Curried function with item in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
itemItem
Returns

Readonly<AsyncIterableIterator<Awaited<Item>>>

Example
1
const repeat3Times = repeat(3);
2
repeat3Times("foo"); // ["foo", "foo", "foo"]

View source


take()

1
function take(
2
amount: Numeric,
3
): <Item>(
4
iterable: IsomorphicIterable<Item>,
5
) => Readonly<AsyncIterableIterator<Awaited<Item>>>;

Take the given amount of items from the iterable or asynchronous iterable.

Parameters
ParameterTypeDescription
amountNumericAmount of items to take.
Returns

Function

Curried function with amount in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Readonly<AsyncIterableIterator<Awaited<Item>>>

Example
1
const take2 = take(2);
2
take2([1, 2, 3, 4, 5]); // [1, 2]

View source


toIterable()

1
function toIterable<ValueOrAsyncIterable>(
2
valueOrIterable: ValueOrAsyncIterable,
3
): Readonly<
4
AsyncIterableIterator<
5
ValueOrAsyncIterable extends IsomorphicIterable ?
6
IsomorphicIterableItem<ValueOrAsyncIterable<ValueOrAsyncIterable>>
7
: ValueOrAsyncIterable
8
>
9
>;

Takes a value, iterable or asynchronous iterable and yields it.

Type parameters
Type parameter
ValueOrAsyncIterable
Parameters
ParameterTypeDescription
valueOrIterableValueOrAsyncIterableVale or iterable to yield.
Returns

Readonly<AsyncIterableIterator<ValueOrAsyncIterable extends IsomorphicIterable ? IsomorphicIterableItem<ValueOrAsyncIterable<ValueOrAsyncIterable>> : ValueOrAsyncIterable>>

Yielded item or iterable.

Example
1
const iterable = toIterable(1);
2
const iterator = getIterator(iterable);
3
iterator.next(); // { value: 1, done: false }
4
iterator.next(); // { value: undefined, done: true }
See

createIterableIterator

View source


unique()

1
function unique<Item>(
2
iterable: IsomorphicIterable<Item>,
3
): Readonly<AsyncIterableIterator<Awaited<Item>>>;

Returns a single instance of each item in the iterable or asynchronous iterable.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
iterableIsomorphicIterable<Item>Iterable to be filtered.
Returns

Readonly<AsyncIterableIterator<Awaited<Item>>>

Generators with a single instance of each item of the iterable.

Example
1
unique([1, 2, 3, 4, 1, 2, 3, 4]); // [1, 2, 3, 4]

View source


zip()

1
function zip<ItemFirst>(
2
iterableFirst: IsomorphicIterable<ItemFirst>,
3
): <ItemSecond>(
4
iterableSecond: IsomorphicIterable<ItemSecond>,
5
) => Readonly<
6
AsyncIterableIterator<readonly [ItemFirst | Awaited<ItemFirst>, ItemSecond]>
7
>;

Takes two iterables or asynchronous iterable and returns a new iterable or asynchronous iterable with the length of the shortest iterable with tuples containing the items from both.

Type parameters
Type parameter
ItemFirst
Parameters
ParameterTypeDescription
iterableFirstIsomorphicIterable<ItemFirst>One of the iterables to be zipped.
Returns

Function

Curried function with iterableFirst in context.

Type parameters
Type parameter
ItemSecond
Parameters
ParameterType
iterableSecondIsomorphicIterable<ItemSecond>
Returns

Readonly<AsyncIterableIterator<readonly [ItemFirst | Awaited<ItemFirst>, ItemSecond]>>

Example
1
const zipNumbers = zip([0, 1, 2]);
2
zipNumbers([3, 4, 5]); // [[0, 3], [1, 4], [2, 5]]

View source


zipIndex()

1
function zipIndex<ItemSecond>(
2
iterableSecond: IsomorphicIterable<ItemSecond>,
3
): Readonly<AsyncIterableIterator<readonly [number, ItemSecond]>>;

Yields a tuple for each item in the iterable with the index of said item.

Type parameters
Type parameter
ItemSecond
Parameters
ParameterType
iterableSecondIsomorphicIterable<ItemSecond>
Returns

Readonly<AsyncIterableIterator<readonly [number, ItemSecond]>>

Example
1
zipIndex(["foo", "bar"]); // [[0, "foo"], [1, "bar"]]
Yields

Tuples with the index of each item.

View source

Asynchronous Reducers

count()

1
function count<Item>(
2
predicate: Filter<Item>,
3
): (iterable: IsomorphicIterable<Item>) => Promise<number>;

Counts the number of items that satisfy a predicate in the given iterable or asynchronous iterable.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
predicateFilter<Item>Predicate function for items to be counted.
Returns

Function

Curried function with predicate in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Promise<number>

Example
1
const countOdds = count((number: number) => number % 2 !== 1);
2
countOdds([1, 2, 3, 4, 5]); // 3
3
countOdds([0, 2, 4, 6, 8]); // 0

View source


entriesToObject()

1
function entriesToObject<Item>(
2
iterable: IsomorphicIterable<Item>,
3
): Promise<Readonly<Record<EntryKey<Item>, EntryValue<Item>>>>;

Takes an entries iterable or asynchronous iterable and returns an object.

Type parameters
Type parameter
Item extends Entry
Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Promise<Readonly<Record<EntryKey<Item>, EntryValue<Item>>>>

Object constructed from entries.

Example
1
entriesToObject([["key", "value"]]); // { key: "value" }
2
entriesToObject([
3
["foo", "bar"],
4
["number", 1],
5
]); // { foo: "bar", number: 1 }

View source


every()

1
function every<Item, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Unary<Item, boolean>
3
: Predicate<Item, Predicated>,
4
): (iterable: IsomorphicIterable<Item>) => Promise<boolean>;

Evaluates items in an iterable or asynchronous iterable against a predicate and returns true if all items evaluates to true.

Type parameters
Type parameterValue
Item-
Predicatednever
Parameters
ParameterTypeDescription
predicateSingle<Predicated> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Predicated>Predicate function to evaluate each item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Promise<boolean>

Example
1
const everyEven = every((number: number) => number % 2 === 0);
2
everyEven([2, 4, 6, 8]); // true
3
everyEven([1, 2, 3, 4]); // false

View source


find()

1
function find<Item>(
2
predicate: Unary<Item, boolean>,
3
): (iterable: IsomorphicIterable<Item>) => Promise<Maybe<Item>>;

Returns the value of the first item in the iterable or asynchronous iterable where predicate is true, undefined otherwise.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
predicateUnary<Item, boolean>Predicate function to search for item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Promise<Maybe<Item>>

Example
1
const findEven = find((number: number) => number % 2 === 0);
2
findEven([1, 2, 3, 4]); // 2
3
findEven([1, 3, 5, 7]); // undefined

View source


groupBy()

1
function groupBy<Item, Key>(
2
grouper: Unary<Item, Key>,
3
): <Iterable>(
4
iterable: Iterable,
5
) => Iterable extends AsyncIterable<unknown> ?
6
Promise<Readonly<Record<Key, ReadOnlyArray<Item>>>>
7
: Readonly<Record<Key, ReadOnlyArray<Item>>>;

Groups values of an iterable or asynchronous iterable in an object based on the output of the grouper function.

Type parameters
Type parameter
Item
Key extends PropertyKey
Parameters
ParameterTypeDescription
grouperUnary<Item, Key>Grouper function.
Returns

Function

Object with grouped values.

Type parameters
Type parameter
Iterable extends IsomorphicIterable<Item>
Parameters
ParameterType
iterableIterable
Returns

Iterable extends AsyncIterable<unknown> ? Promise<Readonly<Record<Key, ReadOnlyArray<Item>>>> : Readonly<Record<Key, ReadOnlyArray<Item>>>

Example
1
const groupByType = groupBy((value: number) =>
2
number % 2 === 0 ? "even" : "odd",
3
);
4
groupByType([1, 2, 3, 4, 5]); // { even: [2, 4], odd: [1, 3, 5] }

View source


1
function head<Iterable>(
2
iterable: Iterable,
3
): Iterable extends ReadOnlyArray ? Head<Iterable<Iterable>>
4
: Maybe<IsomorphicIterableItem<Iterable>>;

Get first element of an iterable or asynchronous iterable (undefined if it is empty).

Type parameters
Type parameter
Iterable extends IsomorphicIterable
Parameters
ParameterTypeDescription
iterableIterableIterable to get the first element from.
Returns

Iterable extends ReadOnlyArray ? Head<Iterable<Iterable>> : Maybe<IsomorphicIterableItem<Iterable>>

First element of the iterable (undefined if empty).

Example
1
head([1, 2, 3]); // 1

View source


includes()

1
function includes<SearchItem>(
2
searchItem: SearchItem,
3
): (iterable: IsomorphicIterable<unknown>) => Promise<boolean>;

Tries to find the given searchItem in iterable or asynchronous iterable.

Type parameters
Type parameter
SearchItem
Parameters
ParameterTypeDescription
searchItemSearchItemItem to search.
Returns

Function

Curried function with searchItem set in context.

Parameters
ParameterType
iterableIsomorphicIterable<unknown>
Returns

Promise<boolean>

Example
1
const includesTwo = includes(2);
2
includesTwo([1, 2, 3, 4]); // true
3
includesTwo([1, 3, 5, 7]); // false

View source


iterableToArray()

1
function iterableToArray<Iterable>(
2
iterable: Iterable,
3
): Promise<ReadOnlyArray<IsomorphicIterableItem<Iterable>>>;

Turns given iterable or asynchronous iterable into an array.

Type parameters
Type parameter
Iterable extends IsomorphicIterable
Parameters
ParameterTypeDescription
iterableIterableIterable to be turned into an array.
Returns

Promise<ReadOnlyArray<IsomorphicIterableItem<Iterable>>>

Array made of iterable items.

Example
1
iterableToArray([1, 2, 3, 4]); // [1, 2, 3, 4]

View source


join()

1
function join<Separator>(
2
separator: Separator,
3
): <Iterable>(
4
iterable: Iterable,
5
) => Iterable extends AsyncIterable<unknown> ?
6
Promise<`${string}${Separator}${string}`>
7
: `${string}${Separator}${string}`;

Takes a separator string and a iterable or asynchronous iterable and returns a string with the concatenation of all the elements separated by the separator.

Type parameters
Type parameter
Separator extends string
Parameters
ParameterTypeDescription
separatorSeparatorString to use as separator.
Returns

Function

Curried function with separator in context.

Type parameters
Type parameter
Iterable extends IsomorphicIterable
Parameters
ParameterType
iterableIterable
Returns

Iterable extends AsyncIterable<unknown> ? Promise<`${string}${Separator}${string}`> : `${string}${Separator}${string}`

Example
1
const joinWithSpaces = join(" ");
2
joinWithSpaces([1, 2, 3]); // "1 2 3"

View source


length()

1
function length<Iterable>(
2
iterable: Iterable,
3
): Iterable extends AsyncIterable<unknown> ? Promise<number> : number;

Get the length of an iterable or asynchronous iterable.

Type parameters
Type parameter
Iterable extends IsomorphicIterable<unknown>
Parameters
ParameterTypeDescription
iterableIterableIterable or asynchronous iterable to get the length from.
Returns

Iterable extends AsyncIterable<unknown> ? Promise<number> : number

Promise with the length of the iterable.

Example
1
length([1, 2, 3]); // 3

View source


reduce()

1
function reduce<Item, Accumulator>(
2
reducer: Unary<Item, Unary<Accumulator, Awaitable<Accumulator>>>,
3
): (
4
initialValue: Accumulator,
5
) => <Iterable>(
6
iterable: Iterable,
7
) => Iterable extends AsyncIterable<unknown> ? Promise<Accumulator>
8
: Accumulator;

Reducer function for iterables and asynchronous iterables.

Type parameters
Type parameter
Item
Accumulator
Parameters
ParameterTypeDescription
reducerUnary<Item, Unary<Accumulator, Awaitable<Accumulator>>>Reducer function.
Returns

Function

Curried function with reducer in context.

Parameters
ParameterType
initialValueAccumulator
Returns

Function

Type parameters
Type parameter
Iterable extends IsomorphicIterable<Item>
Parameters
ParameterType
iterableIterable
Returns

Iterable extends AsyncIterable<unknown> ? Promise<Accumulator> : Accumulator

Example
1
const sum = Accumulator<number>(item => total => total + item);
2
const sumFrom0 = sum(0);
3
4
sumFrom0([1, 2, 3]); // 6

View source


some()

1
function some<Item, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Unary<Item, boolean>
3
: Predicate<Item, Predicated>,
4
): (iterable: IsomorphicIterable<Item>) => Promise<boolean>;

Evaluates items in an iterable or asynchronous iterable against a predicate and returns true if any item evaluates to true.

Type parameters
Type parameterValue
Item-
Predicatednever
Parameters
ParameterTypeDescription
predicateSingle<Predicated> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Predicated>Predicate function to evaluate each item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableIsomorphicIterable<Item>
Returns

Promise<boolean>

Example
1
const someEven = some((number: number) => number % 2 === 0);
2
someEven([1, 2, 3, 4]); // true
3
someEven([1, 3, 5, 7]); // false

View source

Internal

setAsyncIterator()

1
function setAsyncIterator<Value>(
2
value: Value,
3
): <Source>(
4
object: Source,
5
) => Omit<Source, typeof asyncIterator> &
6
Readonly<Record<typeof asyncIterator, Value>>;

Set Symbol.asyncIterator to the given value to the given object.

Type parameters
Type parameter
Value
Parameters
ParameterType
valueValue
Returns

Function

Curried function with key in context.

Type parameters
Type parameter
Source extends object
Parameters
ParameterType
objectSource
Returns

Omit<Source, typeof asyncIterator> & Readonly<Record<typeof asyncIterator, Value>>

Example
1
setAsyncIterator("🟢")({ foo: "bar" }); // { [Symbol.asyncIterator]: "🟢", foo: "bar" }

View source

Synchronous

Common

createIterableIterator()

1
function createIterableIterator<Item>(
2
generatorFunction: () => Readonly<Iterator<Item, void, void>>,
3
): Readonly<IterableIterator<Item>>;

Takes a generator function and returns an iterable iterator object.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
generatorFunction() => Readonly<Iterator<Item, void, void>>Generator to be used every time [Symbol.iterator] is called.
Returns

Readonly<IterableIterator<Item>>

Iterable iterator object.

Example
1
const identityGenerator = function* (value) {
2
yield value;
3
};
4
const iterableIterator = createIterableIterator(identityGenerator);
5
6
const fooIdentity = iterableIterator("foo");
7
8
for (const value of fooIdentity) {
9
console.log(value); // "foo"
10
}
11
12
// Same IterableIterator as above, return values again:
13
14
for (const value of fooIdentity) {
15
console.log(value); // "foo"
16
}

View source


forEach()

1
function forEach<Item>(
2
callback: Unary<Item, void>,
3
): (iterable: Readonly<Iterable<Item>>) => void;

For each function for iterables.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
callbackUnary<Item, void>Function to be called for every item of the iterable.
Returns

Function

Curried function that expects an iterable to loop over and has callback set in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

void

Example
1
const logEach = forEach(console.log);
2
3
logEach([1, 2, 3]); // Logs 1, 2 and 3 separately

View source

Generators

append()

1
function append<TailItem>(
2
tailIterable: Readonly<Iterable<TailItem>>,
3
): <InitialItem>(
4
initialIterable: Readonly<Iterable<InitialItem>>,
5
) => Readonly<IterableIterator<TailItem | InitialItem>>;

Appends one iterable to another.

Type parameters
Type parameter
TailItem
Parameters
ParameterTypeDescription
tailIterableReadonly<Iterable<TailItem>>Iterable to be appended.
Returns

Function

Curried generator function with tailIterable set in context.

Type parameters
Type parameter
InitialItem
Parameters
ParameterType
initialIterableReadonly<Iterable<InitialItem>>
Returns

Readonly<IterableIterator<TailItem | InitialItem>>

Example
1
const appendNumbers = append([0, 1, 2, 3, 4]);
2
3
appendNumbers(["foo", "bar"]); // ["foo", "bar", 0, 1, 2, 3, 4]
4
appendNumbers([]); // [0, 1, 2, 3, 4]

View source


drop()

1
function drop(
2
amount: Numeric,
3
): <Item>(
4
iterable: Readonly<Iterable<Item>>,
5
) => Readonly<IterableIterator<Item>>;

Drop the specified amount of items from the given iterable.

Parameters
ParameterTypeDescription
amountNumericAmount of items to drop.
Returns

Function

Curried function with amount in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<IterableIterator<Item>>

Example
1
const drop2 = drop(2);
2
drop2([1, 2, 3, 4, 5]); // [3, 4, 5]

View source


filter()

1
function filter<Item, Filtered>(
2
predicate: Single<Filtered> extends Single<never> ? Unary<Item, boolean>
3
: Predicate<Item, Filtered>,
4
): (iterable: Readonly<Iterable<Item>>) => Readonly<IterableIterator<Filtered>>;

Filters items in an iterable against a predicate and returns items that evaluated to true.

Type parameters
Type parameterValue
Item-
Filterednever
Parameters
ParameterTypeDescription
predicateSingle<Filtered> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Filtered>Predicate function to evaluate each item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<IterableIterator<Filtered>>

Example
1
const filterEven = filter((number: number) => number % 2 === 0);
2
3
iterableToArray(filterEven([1, 2, 3, 4])); // [2, 4]
4
iterableToArray(filterEven([1, 3, 5, 7])); // []

View source


flat()

1
function flat<Iterable>(iterable: Iterable): Iterable extends (
2
Readonly<Iterable<Item>>
3
) ?
4
Item extends Readonly<Iterable<SubItem>> ?
5
Readonly<IterableIterator<SubItem>>
6
: Readonly<IterableIterator<Item>>
7
: never;

Flattens one level of the given iterable.

Type parameters
Type parameter
Iterable extends Readonly<Iterable<unknown>>
Parameters
ParameterTypeDescription
iterableIterableIterable to flatten.
Returns

Iterable extends Readonly<Iterable<Item>> ? Item extends Readonly<Iterable<SubItem>> ? Readonly<IterableIterator<SubItem>> : Readonly<IterableIterator<Item>> : never

Iterable with flatten items.

Example
1
flat([1, 2, [3, 4]]); // [1, 2, 3, 4]

View source


initial()

1
function initial<Iterable>(
2
iterable: Iterable,
3
): Readonly<
4
IterableIterator<
5
Iterable extends ReadOnlyArray ? Initial<Iterable<Iterable>>[number]
6
: IsomorphicIterableItem<Iterable>
7
>
8
>;

Get all elements except the last one of an iterable.

Type parameters
Type parameter
Iterable extends Readonly<Iterable<unknown>>
Parameters
ParameterTypeDescription
iterableIterableIterable to get the items from.
Returns

Readonly<IterableIterator<Iterable extends ReadOnlyArray ? Initial<Iterable<Iterable>>[number] : IsomorphicIterableItem<Iterable>>>

Iterable with all items except the last one.

Example
1
initial([1, 2, 3]); // [1, 2]

View source


intersperse()

1
function intersperse<Separator>(
2
separator: Separator,
3
): <Item>(
4
iterable: Readonly<Iterable<Item>>,
5
) => Readonly<IterableIterator<Separator | Item>>;

Add the given separator between each element of the given iterable.

Type parameters
Type parameter
Separator
Parameters
ParameterTypeDescription
separatorSeparatorSeparator to add between each element.
Returns

Function

Curried function with separator in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<IterableIterator<Separator | Item>>

Example
1
const intersperseComma = intersperse(",");
2
intersperseComma([1, 2, 3]); // [1, ",", 2, ",", 3]

View source


map()

1
function map<Item, MappedItem>(
2
mapper: Unary<Item, MappedItem>,
3
): (
4
iterable: Readonly<Iterable<Item>>,
5
) => Readonly<IterableIterator<MappedItem>>;

Map for iterables.

Type parameters
Type parameter
Item
MappedItem
Parameters
ParameterTypeDescription
mapperUnary<Item, MappedItem>Mapper function.
Returns

Function

Generator function with mapper function set in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<IterableIterator<MappedItem>>

Example
1
const double = value => value * 2;
2
const mapDouble = map(double);
3
4
mapDouble([1, 2, 3]); // [2, 4, 6]

View source


objectToEntries()

1
function objectToEntries<Key, Value>(
2
input: Readonly<Record<Key, Value>>,
3
): Readonly<IterableIterator<Entry<Key, Value>>>;

Yields all entries of an object (including symbols).

Type parameters
Type parameter
Key extends PropertyKey
Value
Parameters
ParameterTypeDescription
inputReadonly<Record<Key, Value>>Object to get entries from.
Returns

Readonly<IterableIterator<Entry<Key, Value>>>

Iterable with entries of the given object (including symbols).

Example
1
const entries = objectEntries({ a: 1, b: 2 });
2
entries.next(); // { value: ["a", 1], done: false }
3
entries.next(); // { value: ["b", 2], done: false }
4
entries.next(); // { value: undefined, done: true }

View source


prepend()

1
function prepend<InitialItem>(
2
initialIterable: Readonly<Iterable<InitialItem>>,
3
): <TailItem>(
4
tailIterable: Readonly<Iterable<TailItem>>,
5
) => Readonly<IterableIterator<InitialItem | TailItem>>;

Prepends one iterable to another.

Type parameters
Type parameter
InitialItem
Parameters
ParameterTypeDescription
initialIterableReadonly<Iterable<InitialItem>>Iterable to be appended.
Returns

Function

Curried generator function with initialIterable set in context.

Type parameters
Type parameter
TailItem
Parameters
ParameterType
tailIterableReadonly<Iterable<TailItem>>
Returns

Readonly<IterableIterator<InitialItem | TailItem>>

Example
1
const prependNumbers = prepend([0, 1, 2, 3, 4]);
2
prependNumbers(["foo", "bar"]); // [0, 1, 2, 3, 4, "foo", "bar"]

View source


range()

1
function range<Step>(
2
step: Step,
3
): (
4
from: Step extends bigint ? bigint : number,
5
) => (
6
to: Step extends bigint ? bigint : number,
7
) => Readonly<IterableIterator<Step extends bigint ? bigint : number>>;

Range iterable generator (from from to to).

Type parameters
Type parameter
Step extends Numeric
Parameters
ParameterTypeDescription
stepStepStep size.
Returns

Function

Curried function with step set in context.

Parameters
ParameterType
fromStep extends bigint ? bigint : number
Returns

Function

Parameters
ParameterType
toStep extends bigint ? bigint : number
Returns

Readonly<IterableIterator<Step extends bigint ? bigint : number>>

Example
1
[...range(1)(0)(5)]; // [0, 1, 2, 3, 4, 5]
2
[...range(1)(5)(0)]; // [5, 4, 3, 2, 1, 0]

View source


repeat()

1
function repeat(
2
times: Numeric,
3
): <Item>(item: Item) => Readonly<IterableIterator<Item>>;

Repeat given item the specified amount of times (can be BigInt or Infinity times) as an iterable.

Parameters
ParameterType
timesNumeric
Returns

Function

Curried function with item in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
itemItem
Returns

Readonly<IterableIterator<Item>>

Example
1
const repeat3Times = repeat(3);
2
repeat3Times("foo"); // ["foo", "foo", "foo"]

View source


take()

1
function take(
2
amount: Numeric,
3
): <Item>(
4
iterable: Readonly<Iterable<Item>>,
5
) => Readonly<IterableIterator<Item>>;

Take the given amount of items from the iterable.

Parameters
ParameterTypeDescription
amountNumericAmount of items to take.
Returns

Function

Curried function with amount in context.

Type parameters
Type parameter
Item
Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<IterableIterator<Item>>

Example
1
const take2 = take(2);
2
take2([1, 2, 3, 4, 5]); // [1, 2]

View source


toIterable()

1
function toIterable<ValueOrIterable>(
2
valueOrIterable: ValueOrIterable,
3
): Readonly<
4
IterableIterator<
5
ValueOrIterable extends IsomorphicIterable ?
6
IsomorphicIterableItem<ValueOrIterable<ValueOrIterable>>
7
: ValueOrIterable
8
>
9
>;

Takes a value, iterable and yields it.

Type parameters
Type parameterDescription
ValueOrIterableGeneric of value or iterable to yield.
Parameters
ParameterTypeDescription
valueOrIterableValueOrIterableVale or iterable to yield.
Returns

Readonly<IterableIterator<ValueOrIterable extends IsomorphicIterable ? IsomorphicIterableItem<ValueOrIterable<ValueOrIterable>> : ValueOrIterable>>

Yielded item or iterable.

Example
1
const iterable = toIterable(1);
2
const iterator = getIterator(iterable);
3
iterator.next(); // { value: 1, done: false }
4
iterator.next(); // { value: undefined, done: true }
See

createIterableIterator

View source


unique()

1
function unique<Item>(
2
iterable: Readonly<Iterable<Item>>,
3
): Readonly<IterableIterator<Item>>;

Returns a single instance of each item in the iterable.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
iterableReadonly<Iterable<Item>>Iterable to be filtered.
Returns

Readonly<IterableIterator<Item>>

Generators with a single instance of each item of the iterable.

Example
1
unique([1, 2, 3, 4, 1, 2, 3, 4]); // [1, 2, 3, 4]

View source


zip()

1
function zip<ItemFirst>(
2
iterableFirst: Readonly<Iterable<ItemFirst>>,
3
): <ItemSecond>(
4
iterableSecond: Readonly<Iterable<ItemSecond>>,
5
) => Readonly<IterableIterator<readonly [ItemFirst, ItemSecond]>>;

Takes two iterables and returns a new iterable with the length of the shortest iterable with tuples containing the items from both.

Type parameters
Type parameter
ItemFirst
Parameters
ParameterTypeDescription
iterableFirstReadonly<Iterable<ItemFirst>>One of the iterables to be zipped.
Returns

Function

Curried function with iterableFirst in context.

Type parameters
Type parameter
ItemSecond
Parameters
ParameterType
iterableSecondReadonly<Iterable<ItemSecond>>
Returns

Readonly<IterableIterator<readonly [ItemFirst, ItemSecond]>>

Example
1
const zipNumbers = zip([0, 1, 2]);
2
zipNumbers([3, 4, 5]); // [[0, 3], [1, 4], [2, 5]]

View source


zipIndex()

1
function zipIndex<ItemSecond>(
2
iterableSecond: Readonly<Iterable<ItemSecond>>,
3
): Readonly<IterableIterator<readonly [number, ItemSecond]>>;

Yields a tuple for each item in the iterable with the index of said item.

Type parameters
Type parameter
ItemSecond
Parameters
ParameterType
iterableSecondReadonly<Iterable<ItemSecond>>
Returns

Readonly<IterableIterator<readonly [number, ItemSecond]>>

Example
1
zipIndex(["foo", "bar"]); // [[0, "foo"], [1, "bar"]]
Yields

Tuples with the index of each item.

View source

Internal

setIterator()

1
function setIterator<Value>(
2
value: Value,
3
): <Source>(
4
object: Source,
5
) => Omit<Source, typeof iterator> & Readonly<Record<typeof iterator, Value>>;

Set Symbol.iterator to the given value to the given object.

Type parameters
Type parameter
Value
Parameters
ParameterType
valueValue
Returns

Function

Curried function with key in context.

Type parameters
Type parameter
Source extends object
Parameters
ParameterType
objectSource
Returns

Omit<Source, typeof iterator> & Readonly<Record<typeof iterator, Value>>

Example
1
setIterator("🟢")({ foo: "bar" }); // { [Symbol.iterator]: "🟢", foo: "bar" }

View source

Reducers

count()

1
function count<Item>(
2
predicate: Filter<Item>,
3
): (iterable: Readonly<Iterable<Item>>) => number;

Counts the number of items that satisfy a predicate in the given iterable.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
predicateFilter<Item>Predicate function for items to be counted.
Returns

Function

Curried function with predicate in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

number

Example
1
const countOdds = count((number: number) => number % 2 !== 1);
2
countOdds([1, 2, 3, 4, 5]); // 3
3
countOdds([0, 2, 4, 6, 8]); // 0

View source


entriesToObject()

1
function entriesToObject<Item>(
2
iterable: Readonly<Iterable<Item>>,
3
): Readonly<Record<EntryKey<Item>, EntryValue<Item>>>;

Takes an entries iterable and returns an object.

Type parameters
Type parameter
Item extends Entry
Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<Record<EntryKey<Item>, EntryValue<Item>>>

Object constructed from entries.

Example
1
entriesToObject([["key", "value"]]); // { key: "value" }
2
entriesToObject([
3
["foo", "bar"],
4
["number", 1],
5
]); // { foo: "bar", number: 1 }

View source


every()

1
function every<Item, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Unary<Item, boolean>
3
: Predicate<Item, Predicated>,
4
): (iterable: Readonly<Iterable<Item>>) => boolean;

Evaluates items in an iterable against a predicate and returns true if all items evaluates to true.

Type parameters
Type parameterValue
Item-
Predicatednever
Parameters
ParameterTypeDescription
predicateSingle<Predicated> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Predicated>Predicate function to evaluate each item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

boolean

Example
1
const everyEven = every((number: number) => number % 2 === 0);
2
everyEven([2, 4, 6, 8]); // true
3
everyEven([1, 2, 3, 4]); // false

View source


find()

1
function find<Item>(
2
predicate: Unary<Item, boolean>,
3
): (iterable: Readonly<Iterable<Item>>) => Maybe<Item>;

Returns the value of the first item in the iterable where predicate is true, undefined otherwise.

Type parameters
Type parameter
Item
Parameters
ParameterTypeDescription
predicateUnary<Item, boolean>Predicate function to search for item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Maybe<Item>

Example
1
const findEven = find((number: number) => number % 2 === 0);
2
findEven([1, 2, 3, 4]); // 2
3
findEven([1, 3, 5, 7]); // undefined

View source


groupBy()

1
function groupBy<Item, Key>(
2
grouper: Unary<Item, Key>,
3
): (
4
iterable: Readonly<Iterable<Item>>,
5
) => Readonly<Record<Key, ReadOnlyArray<Item>>>;

Groups values of an iterable in an object based on the output of the grouper function.

Type parameters
Type parameter
Item
Key extends PropertyKey
Parameters
ParameterTypeDescription
grouperUnary<Item, Key>Grouper function.
Returns

Function

Object with grouped values.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Readonly<Record<Key, ReadOnlyArray<Item>>>

Example
1
const groupByType = groupBy((value: number) =>
2
number % 2 === 0 ? "even" : "odd",
3
);
4
groupByType([1, 2, 3, 4, 5]); // { even: [2, 4], odd: [1, 3, 5] }

View source


head()

1
function head<Iterable>(
2
iterable: Iterable,
3
): Iterable extends ReadOnlyArray ? Head<Iterable<Iterable>>
4
: Maybe<IsomorphicIterableItem<Iterable>>;

Get first element of an iterable (undefined if it is empty).

Type parameters
Type parameter
Iterable extends Readonly<Iterable<unknown>>
Parameters
ParameterTypeDescription
iterableIterableIterable to get the first element from.
Returns

Iterable extends ReadOnlyArray ? Head<Iterable<Iterable>> : Maybe<IsomorphicIterableItem<Iterable>>

First element of the iterable (undefined if empty).

Example
1
head([1, 2, 3]); // 1

View source


includes()

1
function includes<SearchItem>(
2
searchItem: SearchItem,
3
): (iterable: Readonly<Iterable<unknown>>) => boolean;

Tries to find the given searchItem in iterable.

Type parameters
Type parameter
SearchItem
Parameters
ParameterTypeDescription
searchItemSearchItemItem to search.
Returns

Function

Curried function with searchItem set in context.

Parameters
ParameterType
iterableReadonly<Iterable<unknown>>
Returns

boolean

Example
1
const includesTwo = includes(2);
2
includesTwo([1, 2, 3, 4]); // true
3
includesTwo([1, 3, 5, 7]); // false

View source


iterableToArray()

1
function iterableToArray<Iterable>(
2
iterable: Iterable,
3
): ReadOnlyArray<IsomorphicIterableItem<Iterable>>;

Turns given iterable into an array.

Type parameters
Type parameter
Iterable extends Readonly<Iterable<unknown>>
Parameters
ParameterTypeDescription
iterableIterableIterable to be turned into an array.
Returns

ReadOnlyArray<IsomorphicIterableItem<Iterable>>

Array made of iterable items.

Example
1
iterableToArray([1, 2, 3, 4]); // [1, 2, 3, 4]

View source


join()

1
function join<Separator>(
2
separator: Separator,
3
): <Item>(
4
iterable: Readonly<Iterable<Item>>,
5
) => `${string}${Separator}${string}`;

Takes a separator string and a iterable and returns a string with the concatenation of all the elements separated by the separator.

Type parameters
Type parameter
Separator extends string
Parameters
ParameterTypeDescription
separatorSeparatorString to use as separator.
Returns

Function

Curried function with separator in context.

Type parameters
Type parameter
Item extends Strigifiable
Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

`${string}${Separator}${string}`

Example
1
const joinWithSpaces = join(" ");
2
joinWithSpaces([1, 2, 3]); // "1 2 3"

View source


length()

1
function length(iterable: Readonly<Iterable<unknown>>): number;

Get the length of an iterable.

Parameters
ParameterTypeDescription
iterableReadonly<Iterable<unknown>>Iterable to get the length from.
Returns

number

Promise with the length of the iterable.

Example
1
length([1, 2, 3]); // 3

View source


reduce()

1
function reduce<Item, Accumulator>(
2
reducer: Reducer<Item, Accumulator>,
3
): (
4
initialValue: Accumulator,
5
) => (iterable: Readonly<Iterable<Item>>) => Accumulator;

Reducer function for iterables.

Type parameters
Type parameter
Item
Accumulator
Parameters
ParameterTypeDescription
reducerReducer<Item, Accumulator>Reducer function.
Returns

Function

Curried function with reducer in context.

Parameters
ParameterType
initialValueAccumulator
Returns

Function

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

Accumulator

Example
1
const sum = reduce<number>(item => total => total + item);
2
const sumFrom0 = sum(0);
3
4
sumFrom0([1, 2, 3]); // 6

View source


some()

1
function some<Item, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Unary<Item, boolean>
3
: Predicate<Item, Predicated>,
4
): (iterable: Readonly<Iterable<Item>>) => boolean;

Evaluates items in an iterable against a predicate and returns true if any item evaluates to true.

Type parameters
Type parameterValue
Item-
Predicatednever
Parameters
ParameterTypeDescription
predicateSingle<Predicated> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Predicated>Predicate function to evaluate each item.
Returns

Function

Curried function with predicate set in context.

Parameters
ParameterType
iterableReadonly<Iterable<Item>>
Returns

boolean

Example
1
const someEven = some((number: number) => number % 2 === 0);
2
someEven([1, 2, 3, 4]); // true
3
someEven([1, 3, 5, 7]); // false

View source