Skip to main content

API

Collections

FunctionDescription

join

Returns the result of concatenating the values of iterable to a string where values are separated by separator.

Like Array.prototype.join, but for iterables, but does not treat null, undefined, or [] specially.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
join(`, `),
),
)
//=> sloth, more sloth, even more sloth

joinAsync

Returns a promise that resolves to the result of concatenating the values of asyncIterable to a string where values are separated by separator.

Like Array.prototype.join, but for async iterables, but does not treat null, undefined, or [] specially.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
joinAsync(`, `),
),
)
//=> sloth, more sloth, even more sloth

joinConcur

Returns a promise that resolves to the result of concatenating the values of concurIterable to a string where values are separated by separator.

Like Array.prototype.join, but for concur iterables, but does not treat null, undefined, or [] specially.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
joinConcur(`, `),
),
)
//=> sloth, more sloth, even more sloth

toArray

Returns a Reducer that collects values to an Array.

Example

console.log(
pipe(
cycle([`sloth`, `more sloth`]),
take(4),
reduce(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'sloth', 'more sloth' ]

toGrouped

Returns a Reducer that reduces key-value pairs using outerReducer and reduces values with the same key using innerReducer.

Example

console.log(
pipe(
[`sloth`, `some sloth`, `sleep`, `more sloth`, `even more sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toArray(), toMap())),
),
)
//=> Map(3) {
//=> 5 => [ 'sloth', 'sleep' ],
//=> 10 => [ 'some sloth', 'more sloth' ],
//=> 15 => [ 'even more sloth' ]
//=> }

toJoin

Returns a Reducer that concatenates values to a string where values are separated by separator.

Joins like Array.prototype.join, but does not treat null, undefined, or [] specially.

Use when composing reducers. Prefer join, joinAsync, and joinConcur for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toJoin(`,`), toMap())),
),
)
//=> Map(2) { 5 => 'sloth,sleep', 10 => 'more sloth,some sloth' }

toMap

Returns a KeyedReducer that collects key-value pairs to a Map.

In the case of pairs with duplicate keys, the value of the last one wins.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
map(string => [string, string.length]),
reduce(toMap()),
),
)
//=> Map(3) { 'sloth' => 5, 'more sloth' => 10, 'even more sloth' => 15 }

toMultiple

Returns a Reducer or OptionalReducer that reduces values to an object or array of the same shape as reducers using all of the reducers in reducers.

Returns an OptionalReducer if at least one of the input reducers is an OptionalReducer. Otherwise, returns a Reducer.

Example

console.log(
pipe(
[`sloth`, `some sloth`, `sleep`, `more sloth`, `even more sloth`],
map(string => string.length),
reduce(toMultiple([toSet(), toCount(), toJoin(`,`)])),
),
)
//=> [ Set(3) { 5, 10, 15 }, 5, '5,10,5,10,15' ]

console.log(
pipe(
[`sloth`, `some sloth`, `sleep`, `more sloth`, `even more sloth`],
map(string => string.length),
reduce(
toMultiple({
set: toSet(),
count: toCount(),
string: toJoin(`,`),
}),
),
),
)
//=> { set: Set(3) { 5, 10, 15 }, count: 5, string: '5,10,5,10,15' }

toObject

Returns a KeyedReducer that collects key-value pairs to an object.

In the case of pairs with duplicate keys, the value of the last one wins.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
map(string => [string, string.length]),
reduce(toObject()),
),
)
//=> { sloth: 5, 'more sloth': 10, 'even more sloth': 15 }

toSet

Returns a Reducer that collects values to a Set.

Example

console.log(
pipe(
cycle([`sloth`, `more sloth`]),
take(4),
reduce(toArray()),
),
)
//=> Set(2) { 'sloth', 'more sloth' }

toWeakMap

Returns a KeyedReducer that collects key-value pairs to a WeakMap.

In the case of pairs with duplicate keys, the value of the last one wins.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
map(string => [{ sloth: string }, string.length]),
reduce(toWeakMap()),
),
)
//=> WeakMap { <items unknown> }

toWeakSet

Returns a Reducer that collects objects to a WeakSet.

Example

console.log(
pipe(
cycle([`sloth`, `more sloth`]),
take(4),
map(string => ({ sloth: string })),
reduce(toWeakSet()),
),
)
//=> WeakSet { <items unknown> }

Core

Type alias, Variable, FunctionDescription

ConcurIterable

Represents a potentially lazy collection of values, each of type Value, that can be iterated over concurrently.

The collection can be iterated by invoking the concur iterable with an apply callback. The callback is applied to each value in the collection, potentially asynchronously, in some order.

Invoking the concur iterable returns a promise that resolves when apply has been applied to each value in the concur iterable and each result returned by apply is awaited.

It is like an event emitter that accepts only one event handler and returns a promise that resolves when all events have been emitted and handled.

Example

const slothNamesConcurIterable = pipe(
asConcur(['sloth-names1.txt', 'sloth-names2.txt']),
mapConcur(filename => fs.promises.readFile(filename, `utf8`)),
flatMapConcur(content => content.split(`\n`)),
)

ConcurIterableApply

The callback invoked for each value of a ConcurIterable.

empty

An iterable that contains zero values.

Can be used as an iterable of any type.

Like [], but opaque.

Example

console.log([...empty])
//=> []

emptyAsync

An async iterable that contains zero values.

Can be used as an async iterable of any type.

Like [], but for async iterables.

Example

console.log(await pipe(emptyAsync, reduceAsync(toArray())))
//=> []

asAsync

Returns an async iterable wrapper around iterable.

Note that when passing a concur iterable the returned async iterable may have to buffer the values produced by the concur iterable because values may not be read from the async iterable as quickly as they are produced by the concur iterable. This is a fundamental problem because concur iterables are "push" based while async iterables are "pull" based, which creates backpressure.

Example

const asyncIterable = asAsync([`sloth`, `more sloth`, `even more sloth`])

console.log(typeof asyncIterable[Symbol.asyncIterator])
//=> function

for await (const value of asyncIterable) {
console.log(value)
}
//=> sloth
//=> more sloth
//=> even more sloth

asConcur

Returns a concur iterable wrapper around iterable.

Example

const concurIterable = asConcur([`sloth`, `more sloth`, `even more sloth`])

await forEachConcur(console.log, concurIterable)
//=> sloth
//=> more sloth
//=> even more sloth

compose

Returns a function that takes a single parameter and pipes it through the given functions.

Example

const screamify = compose(
name => `${name.toUpperCase()}!`,
text => [text, text, text],
array => array.join(` `),
)

console.log(screamify(`sloth`))
// => SLOTH! SLOTH! SLOTH!

curry

Returns a curried version of fn.

Example

function slothLog(a, b, c) {
console.log(`${a} Sloth ${b} Sloth ${c}`)
}

const curriedSlothLog = curry(slothLog)

console.log(curriedSlothLog.name)
//=> slothLog

console.log(curriedSlothLog.length)
//=> 3

curriedSlothLog(`Hello`, `World`, `!`)
curriedSlothLog(`Hello`)(`World`, `!`)
curriedSlothLog(`Hello`, `World`)(`!`)
curriedSlothLog(`Hello`)(`World`)(`!`)
//=> Hello Sloth World Sloth !
//=> Hello Sloth World Sloth !
//=> Hello Sloth World Sloth !
//=> Hello Sloth World Sloth !

emptyConcur

A concur iterable that contains zero values.

Can be used as a concur iterable of any type.

Like [], but for concur iterables.

Example

console.log(await pipe(emptyConcur, reduceConcur(toArray())))
//=> []

opaque

Returns an iterable equivalent, but not referentially equal, to iterable.

opaqueAsync

Returns an async iterable equivalent, but not referentially equal, to asyncIterable.

opaqueConcur

Returns an concur iterable equivalent, but not referentially equal, to concurIterable.

pipe

Returns the result of piping value through the given functions.

Example

console.log(
pipe(
`sloth`,
name => `${name.toUpperCase()}!`,
text => [text, text, text],
array => array.join(` `),
),
)
// => SLOTH! SLOTH! SLOTH!

Filters

FunctionDescription

exclude

Returns an iterable containing the values of iterable in iteration order excluding the values of excluded.

Example

console.log(
pipe(
[`sloth`, `sleep`, `fast`, `slow`, `mean`],
exclude([`mean`, `fast`]),
reduce(toArray()),
),
)
//=> [ 'sloth', 'sleep', 'slow' ]

excludeAsync

Returns an async iterable containing the values of asyncIterable in iteration order excluding the values of excluded.

Example

console.log(
await pipe(
asAsync([`sloth`, `sleep`, `fast`, `slow`, `mean`]),
excludeAsync([`mean`, `fast`]),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'sleep', 'slow' ]

excludeConcur

Returns a concur iterable containing the values of concurIterable in iteration order excluding the values of excluded.

Example

console.log(
await pipe(
asConcur([`sloth`, `sleep`, `fast`, `slow`, `mean`]),
excludeConcur([`mean`, `fast`]),
reduceConcur(toArray()),
),
)
//=> [ 'sloth', 'sleep', 'slow' ]

filter

Returns an iterable that contains the values of iterable in iteration order excluding the values for which fn returns a falsy value.

Like Array.prototype.filter, but for iterables.

Example

console.log(
pipe(
[`sloth party`, `building`, `sloths in trees`, `city`],
filter(string => string.includes(`sloth`)),
reduce(toArray()),
),
)
//=> [ 'sloth party', 'sloths in trees' ]

filterAsync

Returns an async iterable that contains the values of asyncIterable in iteration order excluding the values for which fn returns a value awaitable to a falsy value.

Like Array.prototype.filter, but for async iterables.

Example

console.log(
await pipe(
asAsync([`sloth party`, `building`, `sloths in trees`, `city`]),
filterAsync(string => string.includes(`sloth`)),
reduceAsync(toArray()),
),
)
//=> [ 'sloth party', 'sloths in trees' ]

filterConcur

Returns a concur iterable that contains the values of concurIterable excluding the values for which fn returns a value awaitable to a falsy value.

Like Array.prototype.filter, but for concur iterables.

Example

console.log(
await pipe(
asConcur([`sloth party`, `building`, `sloths in trees`, `city`]),
filterConcur(string => string.includes(`sloth`)),
reduceConcur(toArray()),
),
)
//=> [ 'sloth party', 'sloths in trees' ]

filterMap

Returns an iterable containing the values of iterable transformed by fn in iteration order excluding the values for which fn returns null or undefined.

Example

console.log(
pipe(
[
{ sloth: `sloth party` },
{ notSloth: `building` },
{ sloth: `sloths in trees` },
{ notSloth: `city` },
],
filterMap(object => object.sloth),
reduce(toArray()),
),
)
//=> [ 'sloth party', 'sloths in trees' ]

filterMapAsync

Returns an async iterable containing the values of asyncIterable transformed by fn in iteration order excluding the values for which fn returns a value awaitable to null or undefined.

Example

console.log(
await pipe(
asAsync([
{ sloth: `sloth party` },
{ notSloth: `building` },
{ sloth: `sloths in trees` },
{ notSloth: `city` },
]),
filterMapAsync(object => object.sloth),
reduceAsync(toArray()),
),
)
//=> [ 'sloth party', 'sloths in trees' ]

filterMapConcur

Returns a concur iterable containing the values of concurIterable transformed by fn excluding the values for which fn returns a value awaitable to null or undefined.

Example

console.log(
await pipe(
asConcur([
{ sloth: `sloth party` },
{ notSloth: `building` },
{ sloth: `sloths in trees` },
{ notSloth: `city` },
]),
filterMapConcur(object => object.sloth),
reduceConcur(toArray()),
),
)
//=> [ 'sloth party', 'sloths in trees' ]

find

Returns an iterable containing the first value of iterable for which fn returns a truthy value. Otherwise, returns an empty iterable.

Like Array.prototype.find, but for iterables.

Example

const iterable = [1, 2, `sloth`, 4, `other string`]

console.log(
pipe(
iterable,
find(value => typeof value === `string`),
or(() => `yawn!`),
)
)
//=> sloth

console.log(
pipe(
iterable,
find(value => Array.isArray(value)),
or(() => `yawn!`),
)
)
//=> yawn!

findAsync

Returns an async iterable containing the first value of asyncIterable for which fn returns a value awaitable to a truthy value. Otherwise, returns an empty async iterable.

Like Array.prototype.find, but for async iterables.

Example

const asyncIterable = asAsync([1, 2, `sloth`, 4, `other string`])

console.log(
await pipe(
asyncIterable,
findAsync(value => typeof value === `string`),
orAsync(() => `yawn!`),
)
)
//=> sloth

console.log(
await pipe(
asyncIterable,
findAsync(value => Array.isArray(value)),
orAsync(() => `yawn!`),
)
)
//=> yawn!

findConcur

Returns a concur iterable containing the first value of concurIterable for which fn returns a value awaitable to a truthy value. Otherwise, returns an empty concur iterable.

Like Array.prototype.find, but for concur iterables.

Example

const concurIterable = asConcur([1, 2, `sloth`, 4, `other string`])

console.log(
await pipe(
concurIterable,
findConcur(value => typeof value === `string`),
orConcur(() => `yawn`),
),
)
//=> sloth

console.log(
await pipe(
concurIterable,
findConcur(value => Array.isArray(value)),
orConcur(() => `yawn`),
),
)
//=> yawn!

findLast

Returns an iterable containing the last value of iterable for which fn returns a truthy value. Otherwise, returns an empty iterable.

Example

const iterable = [1, 2, `sloth`, 4, `other string`]

console.log(
pipe(
iterable,
findLast(value => typeof value === `string`),
or(() => `yawn!`),
),
)
//=> other string

console.log(
pipe(
iterable,
findLast(value => Array.isArray(value)),
or(() => `yawn!`),
),
)
//=> yawn!

findLastAsync

Returns an async iterable containing the last value of asyncIterable for which fn returns a value awaitable to a truthy value. Otherwise, returns an empty async iterable.

Example

const asyncIterable = asAsync([1, 2, `sloth`, 4, `other string`])

console.log(
await pipe(
asyncIterable,
findLastAsync(value => typeof value === `string`),
orAsync(() => `yawn!`),
),
)
//=> other string

console.log(
await pipe(
asyncIterable,
findLastAsync(value => Array.isArray(value)),
orAsync(() => `yawn!`),
),
)
//=> yawn!

findLastConcur

Returns a concur iterable containing the last value of concurIterable for which fn returns a value awaitable to a truthy value. Otherwise, returns an empty concur iterable.

Example

const concurIterable = asConcur([1, 2, `sloth`, 4, `other string`])

console.log(
await pipe(
concurIterable,
findLastConcur(value => typeof value === `string`),
orConcur(() => `yawn!`),
),
)
//=> other string

console.log(
await pipe(
concurIterable,
findLastConcur(value => Array.isArray(value)),
orConcur(() => `yawn!`),
),
)
//=> yawn!

unique

Returns an iterable containing the values of iterable in iteration order, except values are deduplicated if they are equal using Object.is.

Example

console.log(
pipe(
[`sloth`, `not sloth`, `sloth`],
unique,
reduce(toArray()),
),
)
//=> [ 'sloth', 'not sloth' ]

uniqueAsync

Returns an async iterable containing the values of asyncIterable in iteration order, except values are deduplicated if they are equal using Object.is.

Example

console.log(
await pipe(
asAsync([`sloth`, `not sloth`, `sloth`]),
uniqueAsync,
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'not sloth' ]

uniqueBy

Returns an iterable containing the values of iterable in iteration order, except values for which fn returns the same value are deduplicated.

When values are deduplicated, the value earlier in iteration order wins.

Example

console.log(
pipe(
[`sloth`, `sleep`, `fast`, `slow`, `mean`],
uniqueBy(word => word.length),
reduce(toArray()),
),
)
//=> [ 'sloth', 'fast' ]

uniqueByAsync

Returns an async iterable containing the values of asyncIterable in iteration order, except values for which fn returns a value awaitable to the same value are deduplicated.

When values are deduplicated, the value earlier in iteration order wins.

Example

console.log(
await pipe(
asAsync([`sloth`, `sleep`, `fast`, `slow`, `mean`]),
uniqueByAsync(word => word.length),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'fast' ]

uniqueByConcur

Returns a concur iterable containing the values of concurIterable, except values for which fn returns a value awaitable to the same value are deduplicated.

When values are deduplicated, the value earlier in iteration order wins.

Example

console.log(
await pipe(
asConcur([`sloth`, `sleep`, `fast`, `slow`, `mean`]),
uniqueByConcur(word => word.length),
reduceConcur(toArray()),
),
)
//=> [ 'sloth', 'fast' ]

uniqueConcur

Returns a concur iterable containing the values of concurIterable in iteration order, except values are deduplicated if they are equal using Object.is.

Example

console.log(
await pipe(
asConcur([`sloth`, `not sloth`, `sloth`]),
uniqueConcur,
reduceConcur(toArray()),
),
)
//=> [ 'sloth', 'not sloth' ]

Generators

Type alias, FunctionDescription

RangeIterable

An iterable that yields integers in a range. Has a method for obtaining a new iterable that skips numbers in steps.

cycle

Returns an infinite iterable that repeatedly yields the values of iterable in iteration order.

Example

console.log(
pipe(
cycle([`sloth`, `more sloth`]),
take(6),
join(`, `),
),
)
//=> sloth, more sloth, sloth, more sloth, sloth, more sloth

cycleAsync

Returns an infinite async iterable that repeatedly yields the values of asyncIterable.

Example

console.log(
await pipe(
cycleAsync(asAsync([`sloth`, `more sloth`])),
takeAsync(6),
joinAsync(`, `),
),
)
//=> sloth, more sloth, sloth, more sloth, sloth, more sloth

entries

Returns an iterable containing the entries of object.

This differs from Map.prototype.entries in that the returned iterable can be iterated multiple times and differs from Object.entries in that the returned iterable is opaque.

generate

Returns an infinite iterable that yields seed for its first value and then yields the result of applying fn to its previously yielded value for every subsequent value.

Example

console.log(
pipe(
generate(previousValue => previousValue + previousValue, `sloth`),
take(3),
reduce(toArray()),
),
)
//=> [ 'sloth', 'slothsloth', 'slothslothslothsloth' ]

generateAsync

Returns an infinite async iterable that yields seed for its first value and then yields the awaited result of applying fn to its previously yielded value for every subsequent value.

Example

console.log(
await pipe(
generateAsync(previousValue => previousValue + previousValue, `sloth`),
takeAsync(3),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'slothsloth', 'slothslothslothsloth' ]

keys

Returns an iterable containing the keys of object.

This differs from Map.prototype.keys in that the returned iterable can be iterated multiple times and differs from Object.keys in that the returned iterable is opaque.

rangeTo

Returns a RangeIterable that yields the integers between start and end including start and end.

Throws

if either start or end is not an integer.

Example

console.log([...rangeTo(0, 6)])
//=> [ 0, 1, 2, 3, 4, 5, 6 ]

console.log([...rangeTo(0, 6).step(2)])
//=> [ 0, 2, 4, 6 ]

rangeUntil

Returns a RangeIterable that yields the integers between start and end including start, but excluding end.

Throws

if either start or end is not an integer.

Example

console.log([...rangeUntil(0, 6)])
//=> [ 0, 1, 2, 3, 4, 5 ]

console.log([...rangeUntil(0, 6).step(2)])
//=> [ 0, 2, 4 ]

repeat

Returns an infinite iterable that repeatedly yields value.

Example

console.log(
pipe(
repeat(`sloth`),
take(3),
join(`, `),
),
)
//=> sloth, sloth, sloth

values

Returns an iterable containing the values of object.

This differs from Map.prototype.values and Set.prototype.values in that the returned iterable can be iterated multiple times and differs from Object.values in that the returned iterable is opaque.

Optionals

Type alias, FunctionDescription

AsyncOptional

An async iterable containing exactly zero or one values.

ConcurOptional

A concur iterable containing exactly zero or one values.

Optional

An iterable containing exactly zero or one values.

get

Returns the only value in iterable if it contains exactly one value. Otherwise, throws an error.

Example

console.log(get([`sloth`]))
//=> sloth

try {
console.log(get([]))
} catch {
console.log(`Oh no! It was empty...`)
}
//=> Oh no! It was empty...

try {
console.log(get([1, `sloth`, 3]))
} catch {
console.log(`Oh no! It had more than one value...`)
}
//=> Oh no! It had more than one value...

getAsync

Returns a promise that resolves to the only value in asyncIterable if it contains exactly one value. Otherwise, returns a promise that rejects.

Example

console.log(await getAsync(asAsync([`sloth`])))
//=> sloth

try {
console.log(await getAsync(emptyAsync))
} catch {
console.log(`Oh no! It was empty...`)
}
//=> Oh no! It was empty...

try {
console.log(await getAsync(asAsync([1, `sloth`, 3])))
} catch {
console.log(`Oh no! It had more than one value...`)
}
//=> Oh no! It had more than one value...

getConcur

Returns a promise that resolves to the only value in concurIterable if it contains exactly one value. Otherwise, returns a promise that rejects.

Example

console.log(await getConcur(asConcur([`sloth`])))
//=> sloth

try {
console.log(await getConcur(emptyConcur))
} catch {
console.log(`Oh no! It was empty...`)
}
//=> Oh no! It was empty...

try {
console.log(await getConcur(asConcur([1, `sloth`, 3])))
} catch {
console.log(`Oh no! It had more than one value...`)
}
//=> Oh no! It had more than one value...

next

Returns a pair of iterables. If iterable is empty, then both of the returned iterables are empty. Otherwise, the first iterable contains the first value of iterable and the second iterable contains the rest of the values of iterable. The second iterable can only be iterated once.

Example

const slothActivities = [`sleeping`, `yawning`, `eating`]
const [first, rest] = next(slothActivities)

console.log(get(first))
//=> sleeping

console.log([...rest])
//=> [ 'yawning', 'eating' ]

const badThingsAboutSloths = []
const [first2, rest2] = next(badThingsAboutSloths)

console.log(count(first2))
//=> 0

console.log(count(rest2))
//=> 0

nextAsync

Returns a promise that resolves to a pair of async iterables. If asyncIterable is empty, then both of the returned async iterables are empty. Otherwise, the first async iterable contains the first value of asyncIterable and the second async iterable contains the rest of the values of asyncIterable. The second async iterable can only be iterated once.

Example

const slothActivities = asAsync([`sleeping`, `yawning`, `eating`])
const [first, rest] = await nextAsync(slothActivities)

console.log(await getAsync(first))
//=> sleeping

console.log(await reduceAsync(toArray(), rest))
//=> [ 'yawning', 'eating' ]

const badThingsAboutSloths = emptyAsync
const [first2, rest2] = await nextAsync(badThingsAboutSloths)

console.log(await countAsync(first2))
//=> 0

console.log(await countAsync(rest2))
//=> 0

or

Returns the only value in iterable if it contains exactly one value. Otherwise, returns the result of invoking fn.

Example

console.log(pipe([`sloth`], or(() => `Never called`)))
//=> sloth

console.log(pipe([], or(() => `I get called!`)))
//=> I get called!

console.log(pipe([1, `sloth`, 3], or(() => `I also get called!`)))
//=> I also get called!

orAsync

Returns a promise that resolves to the only value in asyncIterable if it contains exactly one value. Otherwise, returns a promise that resolves to the awaited result of invoking fn.

Example

console.log(await pipe(asAsync([`sloth`]), orAsync(() => `Never called`)))
//=> sloth

console.log(await pipe(emptyAsync, orAsync(() => `I get called!`)))
//=> I get called!

console.log(
await pipe(
asAsync([1, `sloth`, 3]),
orAsync(() => `I also get called!`),
),
)
//=> I also get called!

orConcur

Returns a promise that resolves to the only value in concurIterable if it contains exactly one value. Otherwise, returns a promise that resolves to the awaited result of invoking fn.

Example

console.log(await pipe(asConcur([`sloth`]), orConcur(() => `Never called`)))
//=> sloth

console.log(await pipe(emptyConcur, orConcur(() => `I get called!`)))
//=> I get called!

console.log(
await pipe(
asConcur([1, `sloth`, 3]),
orConcur(() => `I also get called!`),
),
)
//=> I also get called!

Predicates

FunctionDescription

all

Returns true if fn returns a truthy value for all values of iterable. Otherwise returns false.

Like Array.prototype.every, but for iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
all(string => string.length > 8),
),
)
//=> false

allAsync

Returns a promise that resolves to true if fn returns a truthy value or a promise that resolves to a truthy value for all values of asyncIterable. Otherwise returns a promise that resolves to false.

Like Array.prototype.every, but for async iterables.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
allAsync(string => string.length > 8),
),
)
//=> false

allConcur

Returns a promise that resolves to true if fn returns a truthy value or a promise that resolves to a truthy value for all values of concurIterable. Otherwise returns a promise that resolves to false.

Like Array.prototype.every, but for concur iterables.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
allConcur(string => string.length > 8),
),
)
//=> false

any

Returns true if fn returns a truthy value for any value of iterable. Otherwise returns false.

Like Array.prototype.some, but for iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
any(string => string.length > 8),
),
)
//=> true

anyAsync

Returns a promise that resolves to true if fn returns a truthy value or a promise that resolves to a truthy value for any value of asyncIterable. Otherwise returns a promise that resolves to false.

Like Array.prototype.some, but for async iterables.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
anyAsync(string => string.length > 8),
),
)
//=> true

anyConcur

Returns a promise that resolves to true if fn returns a truthy value or a promise that resolves to a truthy value for any value of concurIterable. Otherwise returns a promise that resolves to false.

Like Array.prototype.some, but for concur iterables.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
anyConcur(string => string.length > 8),
),
)
//=> true

includes

Returns true if any value of iterable is equal to searchElement using Object.is. Otherwise returns false.

Like Array.prototype.includes, but for iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
includes(3),
),
)
//=> true

includesAsync

Returns a promise that resolves to true if any value of asyncIterable is equal to searchElement using Object.is. Otherwise returns a promise that resolves to false.

Like Array.prototype.includes, but for async iterables.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
includesAsync(3),
),
)
//=> true

includesConcur

Returns a promise that resolves to true if any value of concurIterable is equal to searchElement using Object.is. Otherwise returns a promise that resolves to false.

Like Array.prototype.includes, but for concur iterables.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
includesConcur(3),
),
)
//=> true

none

Returns true if fn returns a falsy value for all values of iterable. Otherwise returns false.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
none(string => string.length > 8),
),
)
//=> false

noneAsync

Returns a promise that resolves to true if fn returns a falsy value or a promise that resolves to a falsy value for all values of asyncIterable. Otherwise returns a promise that resolves to false.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
noneAsync(string => string.length > 8),
),
)
//=> false

noneConcur

Returns a promise that resolves to true if fn returns a falsy value or a promise that resolves to a falsy value for all values of concurIterable. Otherwise returns a promise that resolves to false.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
noneConcur(string => string.length > 8),
),
)
//=> false

Reducers

Type alias, Variable, FunctionDescription

AsyncFunctionReducer

An async reducer that reduces by combining pairs of values using function application.

AsyncKeyedReducer

An async keyed reducer that reduces by creating an initial accumulator using AsyncKeyedReducer.create and then adding key-value pairs to the accumulator values using AsyncKeyedReducer.add. The async keyed reducer is optionally able to combine pairs of accumulators using AsyncKeyedReducer.combine. The accumulator can be queried for values by key using AsyncKeyedReducer.get.

AsyncOptionalReducer

An async reducer that reduces by combining pairs of values using AsyncOptionalReducer.add and then tranforming the final value using AsyncOptionalReducer.finish.

AsyncReducer

An async reducer that reduces by creating an initial accumulator using AsyncReducer.create, then adding values to the accumulator values using AsyncReducer.add, and then tranforming the final accumulator using AsyncReducer.finish. The async reducer is optionally able to combine pairs of accumulators using AsyncReducer.combine.

FunctionReducer

A reducer that reduces by combining pairs of values using function application.

KeyedReducer

A keyed reducer that reduces by creating an initial accumulator using KeyedReducer.create and then adding key-value pairs to the accumulator values using KeyedReducer.add. The accumulator can be queried for values by key using KeyedReducer.get.

OptionalReducer

A reducer that reduces by combining pairs of values using OptionalReducer.add and then tranforming the final value using OptionalReducer.finish.

RawAsyncKeyedReducer

An async keyed reducer that reduces by creating an initial accumulator using RawAsyncKeyedReducer.create and then adding key-value pairs to the accumulator values using RawAsyncKeyedReducer.add. The async keyed reducer is optionally able to combine pairs of accumulators using RawAsyncKeyedReducer.combine. The accumulator can be queried for values by key using RawAsyncKeyedReducer.get.

RawAsyncOptionalReducerWithFinish

An async reducer that reduces by combining pairs of values using RawAsyncOptionalReducerWithFinish.add and then tranforming the final value using RawAsyncOptionalReducerWithFinish.finish.

RawAsyncOptionalReducerWithoutFinish

An async reducer that reduces by combining pairs of values using RawAsyncOptionalReducerWithoutFinish.add.

RawAsyncReducerWithFinish

An async reducer that reduces by creating an initial accumulator using RawAsyncReducerWithFinish.create, then adding values to the accumulator values using RawAsyncReducerWithFinish.add, and then tranforming the final accumulator using RawAsyncReducerWithFinish.finish. The async reducer is optionally able to combine pairs of accumulators using RawAsyncReducerWithFinish.combine.

RawAsyncReducerWithoutFinish

An async reducer that reduces by creating an initial accumulator using RawAsyncReducerWithoutFinish.create and then adding values to the accumulator values using RawAsyncReducerWithoutFinish.add. The async reducer is optionally able to combine pairs of accumulators using RawAsyncReducerWithoutFinish.combine.

RawKeyedReducer

A keyed reducer that reduces by creating an initial accumulator using RawKeyedReducer.create and then adding key-value pairs to the accumulator values using RawKeyedReducer.add. The accumulator can be queried for values by key using RawKeyedReducer.get.

RawOptionalReducerWithFinish

A reducer that reduces by combining pairs of values using RawOptionalReducerWithFinish.add and then tranforming the final value using RawOptionalReducerWithFinish.finish.

RawOptionalReducerWithoutFinish

A reducer that reduces by combining pairs of values using RawOptionalReducerWithoutFinish.add.

RawReducerWithFinish

A reducer that reduces by creating an initial accumulator using RawReducerWithFinish.create, then adding values to the accumulator values using RawReducerWithFinish.add, and then tranforming the final accumulator using RawReducerWithFinish.finish.

RawReducerWithoutFinish

A reducer that reduces by creating an initial accumulator using RawReducerWithoutFinish.create and then adding values to the accumulator values using RawReducerWithoutFinish.add.

Reducer

A reducer that reduces by creating an initial accumulator using Reducer.create, then adding values to the accumulator values using Reducer.add, and then tranforming the final accumulator using Reducer.finish.

NO_ENTRY

A unique value representing the lack of an entry for some key in a KeyedReducer or AsyncKeyedReducer.

Keyed reducers use this instead of null or undefined because they are valid values. Furthermore, introducing a has method for the purpose of disambiguation would be less performant due to the need to perform the lookup twice when the entry exists: has followed by get for the same key.

mapAsyncReducer

Returns an AsyncReducer equivalent to reducer except its final value is transformed using fn.

mapReducer

Returns a Reducer or OptionalReducer equivalent to reducer except its final value is transformed using fn.

normalizeReducer

Returns a non-raw version of reducer.

reduce

Returns the result of reducing iterable using reducer.

An initial accumulator is created using Reducer.create. Then each value in iterable is added to the accumulator and the current accumulator is updated using Reducer.add. Finally, the resulting accumulator is transformed using Reducer.finish if specified.

If reducer is an optional reducer (no Reducer.create method), then an empty iterable is returned if iterable is empty. Otherwise, an iterable containing the result of reducing using the first value of the iterable as the initial accumulator is returned.

Like Array.prototype.reduce, but for iterables.

Example

console.log(
pipe(
[`Hello`, `Sloth!`, `What`, `an`, `interesting`, `program!`],
reduce((a, b) => `${a} ${b}`),
get,
),
)
//=> Hello Sloth! What an interesting program!

console.log(
pipe(
[`Hello`, `Sloth!`, `What`, `an`, `interesting`, `program!`],
reduce({ create: () => ``, add: (a, b) => `${a} ${b}` }),
),
)
//=> Hello Sloth! What an interesting program!

reduceAsync

Returns the result of reducing the asyncIterable using asyncReducer.

Informally, an initial accumulator is created using AsyncReducer.create. Then each value in asyncIterable is added to the accumulator and the current accumulator is updated using AsyncReducer.add. Finally, the resulting accumulator is transformed using AsyncReducer.finish if specified. Multiple accumulators may be created, added to, and then combined if supported via AsyncReducer.combine and the next value of asyncIterable is ready before promises from AsyncReducer.add resolve.

If asyncReducer is an async optional reducer (no AsyncReducer.create method), then an empty async iterable is returned if asyncIterable is empty. Otherwise, an async iterable containing the result of reducing using the first value of the async iterable as the initial accumulator is returned.

Like Array.prototype.reduce, but for async iterables.

Example

console.log(
await pipe(
asAsync([`Hello`, `World!`, `What`, `an`, `interesting`, `program!`]),
reduceAsync((a, b) => `${a} ${b}`),
getAsync,
),
)
//=> Hello World! What an interesting program!

console.log(
await pipe(
asAsync([`Hello`, `World!`, `What`, `an`, `interesting`, `program!`]),
reduceAsync({ create: () => ``, add: (a, b) => `${a} ${b}` }),
),
)
//=> Hello World! What an interesting program!

reduceConcur

Returns the result of reducing the concurIterable using asyncReducer.

Informally, an initial accumulator is created using AsyncReducer.create. Then each value in concurIterable is added to the accumulator and the current accumulator is updated using AsyncReducer.add. Finally, the resulting accumulator is transformed using AsyncReducer.finish if specified. Multiple accumulators may be created, added to, and then combined if supported via AsyncReducer.combine and the next value of concurIterable is ready before promises from AsyncReducer.add resolve.

If asyncReducer is an async optional reducer (no AsyncReducer.create method), then an empty concur iterable is returned if concurIterable is empty. Otherwise, an concur iterable containing the result of reducing using the first value of the concur iterable as the initial accumulator is returned.

Like Array.prototype.reduce, but for concur iterables.

Example

console.log(
await pipe(
asAsync([`Hello`, `World!`, `What`, `an`, `interesting`, `program!`]),
reduceAsync((a, b) => `${a} ${b}`),
getAsync,
),
)
//=> Hello World! What an interesting program!

console.log(
await pipe(
asAsync([`Hello`, `World!`, `What`, `an`, `interesting`, `program!`]),
reduceAsync({ create: () => ``, add: (a, b) => `${a} ${b}` }),
),
)
//=> Hello World! What an interesting program!

Side effects

FunctionDescription

cache

Returns an iterable equivalent to iterable that iterates over iterable at most once.

Example

const iterable = [`sloth`, `more sloth`, `even more sloth`]
const iterableWithEffects = each(console.log, iterable)

const cachedIterable = cache(iterableWithEffects)

console.log([...cachedIterable])
//=> sloth
//=> more sloth
//=> even more sloth
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log([...cachedIterable])
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

cacheAsync

Returns an async iterable equivalent to asyncIterable that iterates over asyncIterable at most once.

Example

const asyncIterable = asAsync([`sloth`, `more sloth`, `even more sloth`])
const asyncIterableWithEffects = eachAsync(console.log, asyncIterable)

const cachedAsyncIterable = cacheAsync(asyncIterableWithEffects)

console.log(await pipe(cachedAsyncIterable, reduceAsync(toArray())))
//=> sloth
//=> more sloth
//=> even more sloth
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(await pipe(cachedAsyncIterable, reduceAsync(toArray())))
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

cacheConcur

Returns a concur iterable equivalent to concurIterable that iterates over concurIterable at most once.

Example

const concurIterable = asConcur([`sloth`, `more sloth`, `even more sloth`])
const concurIterableWithEffects = eachConcur(console.log, concurIterable)

const cachedConcurIterable = cacheConcur(concurIterableWithEffects)

console.log(await pipe(cachedConcurIterable, reduceConcur(toArray())))
//=> sloth
//=> more sloth
//=> even more sloth
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(await pipe(cachedConcurIterable, reduceConcur(toArray())))
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

consume

Iterates through iterable causing lazy operations to run.

Example

const iterable = pipe(
[`sloth`, 2, 3],
each(console.log),
)
// No output

consume(iterable)
//=> sloth
//=> 2
//=> 3

consumeAsync

Iterates through asyncIterable causing lazy operations to run.

Example

const asyncIterable = pipe(
asAsync([`sloth`, 2, 3]),
eachAsync(console.log),
)
// No output

await consumeAsync(asyncIterable)
//=> sloth
//=> 2
//=> 3

consumeConcur

Iterates through the concurIterable causing lazy operations to run.

Example

const concurIterable = pipe(
asConcur([`sloth`, 2, 3]),
eachConcur(console.log),
)
// No output

await consumeConcur(asyncIterable)
//=> sloth
//=> 2
//=> 3

each

Returns an iterable equivalent to iterable that applies fn to each value of iterable as it is iterated.

Example

const sloths = [`carl`, `frank`, `phil`]

console.log([...each(console.log, sloths)])
//=> carl
//=> frank
//=> phil
//=> [ 'carl', 'frank', 'phil' ]

eachAsync

Returns an async iterable equivalent to asyncIterable that applies fn to each value of asyncIterable as it is iterated.

The result of applying fn to a value is awaited before yielding and then moving on to the next value.

Example

const eachedSloths = await pipe(
asAsync([`carl`, `frank`, `phil`]),
eachAsync(console.log),
reduceAsync(toArray()),
)
//=> carl
//=> frank
//=> phil

console.log(eachedSloths)
//=> [ 'carl', 'frank', 'phil' ]

eachConcur

Returns an concur iterable equivalent to concurIterable that applies fn to each value of concurIterable as it is iterated.

The result of applying fn to a value is awaited before yielding.

Example

const eachedSloths = await pipe(
asConcur([`carl`, `frank`, `phil`]),
eachConcur(console.log),
reduceConcur(toArray()),
)
//=> carl
//=> frank
//=> phil

console.log(eachedSloths)
//=> [ 'carl', 'frank', 'phil' ]

forEach

Applies fn to each value of iterable.

Like Array.prototype.forEach, but for iterables.

Example

const sloths = [`carl`, `frank`, `phil`]

forEach(console.log, sloths)
//=> carl
//=> frank
//=> phil

forEachAsync

Returns a promise that resolves when fn has been applied to each value of asyncIterable and the result of each application has been awaited.

The result of applying fn to a value is awaited before moving on to the next value.

Like Array.prototype.forEach, but for async iterables.

Example

const sloths = asAsync([`carl`, `frank`, `phil`])

await forEachAsync(console.log, sloths)
//=> carl
//=> frank
//=> phil

forEachConcur

Returns a promise that resolves when fn has been applied to each value of concurIterable and the result of each application has been awaited.

Like Array.prototype.forEach, but for concur iterables.

Example

const sloths = asConcur([`carl`, `frank`, `phil`])

await forEachConcur(console.log, sloths)
//=> carl
//=> frank
//=> phil
//

Splices

Type alias, FunctionDescription

WindowOptions

Options for window, windowAsync, and windowConcur.

at

Returns an iterable containing the value at the given index of iterable or an empty iterable if index is out of bounds.

WARNING: This function linearly iterates up to index because iterables do not support random access.

Throws

if index is not a non-negative integer.

Example

const iterable = [`sloth`, `more sloth`, `even more sloth`]

console.log(
pipe(
iterable,
at(1),
get,
),
)
//=> 'more sloth'

atAsync

Returns an async iterable containing the value at the given index of asyncIterable or an empty async iterable if index is out of bounds.

WARNING: This function linearly iterates up to index because async iterables do not support random access.

Throws

if index is not a non-negative integer.

Example

const asyncIterable = asAsync([`sloth`, `more sloth`, `even more sloth`])

console.log(
await pipe(
asyncIterable,
atAsync(1),
getAsync,
),
)
//=> 'more sloth'

atConcur

Returns a concur iterable containing the value at the given index of concurIterable in iteration order, or an empty concur iterable if index is out of bounds.

WARNING: This function linearly iterates up to index because concur iterables do not support random access.

Throws

if index is not a non-negative integer.

Example

const concurIterable = asConcur([`sloth`, `more sloth`, `even more sloth`])

console.log(
await pipe(
concurIterable,
atConcur(1),
getConcur,
),
)
//=> 'more sloth'

chunk

Returns an iterable equivalent to iterable except its values are grouped into arrays that each contain size values.

The last array in the returned iterable will contain fewer than size values (but at least one) if the number of values in iterable is not divisible by size.

Throws

if size is not a positive integer.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
chunk(3),
reduce(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

console.log(
pipe(
[`S`, `L`, `O`, `T`, `H`],
chunk(2),
reduce(toArray()),
),
)
//=> [ [ 'S', 'L' ], [ 'O', 'T' ], [ 'H' ] ]

chunkAsync

Returns an async iterable equivalent to asyncIterable except its values are grouped into arrays that each contain size values.

The last array in the returned async iterable will contain fewer than size values (but at least one) if the number of values in asyncIterable is not divisible by size.

Throws

if size is not a positive integer.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, 7, 8, 9]),
chunkAsync(3),
reduceAsync(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

console.log(
await pipe(
asAsync([`S`, `L`, `O`, `T`, `H`]),
chunkAsync(2),
reduceAsync(toArray()),
),
)
//=> [ [ 'S', 'L' ], [ 'O', 'T' ], [ 'H' ] ]

chunkConcur

Returns a concur iterable equivalent to concurIterable except its values are grouped into arrays that each contain size values.

The last array in the returned concur iterable will contain fewer than size values (but at least one) if the number of values in concurIterable is not divisible by size.

Throws

if size is not a positive integer.

Example

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, 6, 7, 8, 9]),
chunkConcur(3),
reduceConcur(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

console.log(
await pipe(
asConcur([`S`, `L`, `O`, `T`, `H`]),
chunkConcur(2),
reduceConcur(toArray()),
),
)
//=> [ [ 'S', 'L' ], [ 'O', 'T' ], [ 'H' ] ]

concat

Returns an iterable that contains the values of each iterable in iterables in iteration order.

Like Array.prototype.concat, but for iterables.

Example

console.log(
pipe(
concat([1, 2], [3, `sloth`, 5], [6, 7]),
reduce(toArray()),
),
)
//=> [ 1, 2, 3, 'sloth', 5, 6, 7 ]

concatAsync

Returns an async iterable that contains the values of each iterable in iterables in iteration order.

Like Array.prototype.concat, but for async iterables.

Example

console.log(
await pipe(
concatAsync(asAsync([1, 2]), [3, `sloth`, 5], asAsync([6, 7])),
reduceAsync(toArray()),
),
)
//=> [ 1, 2, 3, 'sloth', 5, 6, 7 ]

concatConcur

Returns a concur iterable that contains the values of each iterable in iterables.

Like Array.prototype.concat, but for concur iterables.

Example

console.log(
await pipe(
concatConcur(asAsync([1, 2]), [3, `sloth`, 5], asConcur([6, 7])),
reduceConcur(toArray()),
),
)
//=> [ 1, 2, 3, 'sloth', 5, 6, 7 ]

drop

Returns an iterable containing the values of iterable in iteration order except for the first count values.

If the count is greater than the number of values in iterable, then an empty iterable is returned.

Throws

if count isn't a non-negative integer.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, `sloth`],
drop(3),
reduce(toArray()),
),
)
//=> [ 4, 5, 'sloth' ]

dropAsync

Returns an async iterable containing the values of asyncIterable in iteration order except for the first count values.

If the count is greater than the number of values in asyncIterable, then an empty async iterable is returned.

Throws

if count isn't a non-negative integer.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, `sloth`]),
dropAsync(3),
reduceAsync(toArray()),
),
)
//=> [ 4, 5, 'sloth' ]

dropConcur

Returns a concur iterable containing the values of concurIterable in iteration order except for the first count values.

If the count is greater than the number of values in concurIterable, then an empty concur iterable is returned.

Throws

if count isn't a non-negative integer.

Example

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, `sloth`]),
dropConcur(3),
reduceConcur(toArray()),
),
)
//=> [ 4, 5, 'sloth' ]

dropWhile

Returns an iterable containing the values of iterable in iteration order starting with the first value for which fn returns a falsy value.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, 6, 7, 8, `sloth`],
dropWhile(value => value < 5),
reduce(toArray()),
),
)
//=> [ 5, 6, 7, 8, 'sloth' ]

dropWhileAsync

Returns an async iterable containing the values of asyncIterable in iteration order starting with the first value for which fn returns a value awaitable to a falsy value.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, 7, 8, `sloth`]),
dropWhileAsync(value => value < 5),
reduceAsync(toArray()),
),
)
//=> [ 5, 6, 7, 8, 'sloth' ]

dropWhileConcur

Returns a concur iterable containing the values of concurIterable in iteration order starting with the first value for which fn returns a value awaitable to a falsy value.

Example

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, 6, 7, 8, `sloth`]),
dropWhileConcur(value => value < 5),
reduceConcur(toArray()),
),
)
//=> [ 5, 6, 7, 8, 'sloth' ]

first

Returns an iterable containing the first value of iterable, or an empty iterable if iterable is empty.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
first,
reduce(toArray()),
),
)
//=> [ 'sloth' ]

firstAsync

Returns an async iterable containing the first value of asyncIterable, or an empty async iterable if asyncIterable is empty.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
firstAsync,
reduceAsync(toArray()),
),
)
//=> [ 'sloth' ]

firstConcur

Returns a concur iterable containing the first value of concurIterable, or an empty concur iterable if concurIterable is empty.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
firstConcur,
reduceConcur(toArray()),
),
)
//=> [ 'sloth' ]

last

Returns an iterable containing the last value of iterable, or an empty iterable if iterable is empty.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
last,
reduce(toArray()),
),
)
//=> [ 'even more sloth' ]

lastAsync

Returns an async iterable containing the last value of asyncIterable, or an empty async iterable if asyncIterable is empty.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
lastAsync,
reduceAsync(toArray()),
),
)
//=> [ 'even more sloth' ]

lastConcur

Returns a concur iterable containing the last value of concurIterable, or an empty concur iterable if concurIterable is empty.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
lastConcur,
reduceConcur(toArray()),
),
)
//=> [ 'even more sloth' ]

slice

Returns an iterable containing the values of iterable between start and end (exclusive) of iterable.

If any part of the range between start and end is outside the bounds of the iterable, then that part is excluded from the returned iterable. Thus, the returned iterable may be empty.

WARNING: This function linearly iterates up to end because iterables do not support random access.

Throws

if either start or end is not a non-negative integer, or if start is greater than end.

Example

const iterable = [`sloth`, `more sloth`, `even more sloth`]

console.log(
pipe(
iterable,
slice(0, 3),
reduce(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
pipe(
iterable,
slice(0, 42),
reduce(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
pipe(
iterable,
slice(1, 3),
reduce(toArray()),
),
)
//=> [ 'more sloth', 'even more sloth' ]

console.log(
pipe(
iterable,
slice(3, 5),
reduce(toArray()),
),
)
//=> []

sliceAsync

Returns an async iterable containing the values of asyncIterable between start and end (exclusive) of asyncIterable.

If any part of the range between start and end is outside the bounds of the async iterable, then that part is excluded from the returned async iterable. Thus, the returned async iterable may be empty.

WARNING: This function linearly iterates up to end because async iterables do not support random access.

Throws

if either start or end is not a non-negative integer, or if start is greater than end.

Example

const asyncIterable = asAsync([`sloth`, `more sloth`, `even more sloth`])

console.log(
await pipe(
asyncIterable,
sliceAsync(0, 3),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
await pipe(
asyncIterable,
sliceAsync(0, 42),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
await pipe(
asyncIterable,
sliceAsync(1, 3),
reduceAsync(toArray()),
),
)
//=> [ 'more sloth', 'even more sloth' ]

console.log(
await pipe(
asyncIterable,
sliceAsync(3, 5),
reduceAsync(toArray()),
),
)
//=> []

sliceConcur

Returns a concur iterable containing the values of concurIterable between start and end (exclusive) of concurIterable in iteration order.

If any part of the range between start and end is outside the bounds of the concur iterable, then that part is excluded from the returned concur iterable. Thus, the returned concur iterable may be empty.

WARNING: This function linearly iterates up to end because concur iterables do not support random access.

Throws

if either start or end is not a non-negative integer, or if start is greater than end.

Example

const concurIterable = asConcur([`sloth`, `more sloth`, `even more sloth`])

console.log(
await pipe(
concurIterable,
sliceConcur(0, 3),
reduceConcur(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
await pipe(
concurIterable,
sliceConcur(0, 42),
reduceConcur(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
await pipe(
concurIterable,
sliceConcur(1, 3),
reduceConcur(toArray()),
),
)
//=> [ 'more sloth', 'even more sloth' ]

console.log(
await pipe(
concurIterable,
sliceConcur(3, 5),
reduceConcur(toArray()),
),
)
//=> []

take

Returns an iterable containing the first count values of iterable in iteration order.

If the count is greater than the number of values in iterable, then an iterable equivalent iterable is returned.

Throws

if count isn't a non-negative integer.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, `sloth`],
take(3),
reduce(toArray()),
),
)
//=> [ 1, 2, 3 ]

takeAsync

Returns an async iterable containing the first count values of asyncIterable in iteration order.

If the count is greater than the number of values in asyncIterable, then an async iterable equivalent asyncIterable is returned.

Throws

if count isn't a non-negative integer.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, `sloth`]),
takeAsync(3),
reduceAsync(toArray()),
),
)
//=> [ 1, 2, 3 ]

takeConcur

Returns a concur iterable containing the first count values of concurIterable in iteration order.

If the count is greater than the number of values in concurIterable, then a concur iterable equivalent concurIterable is returned.

Throws

if count isn't a non-negative integer.

Example

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, `sloth`]),
takeConcur(3),
reduceConcur(toArray()),
),
)
//=> [ 1, 2, 3 ]

takeWhile

Returns an iterable containing the values of iterable in iteration order up until but not including the first value for which fn returns a falsy value.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, 6, 7, 8, `sloth`],
takeWhile(value => value < 5),
reduce(toArray()),
),
)
//=> [ 1, 2, 3, 4 ]

takeWhileAsync

Returns an async iterable containing the values of asyncIterable in iteration order up until but not including the first value for which fn returns a value awaitable to a falsy value.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, 7, 8, `sloth`]),
takeWhileAsync(value => value < 5),
reduceAsync(toArray()),
),
)
//=> [ 1, 2, 3, 4 ]

takeWhileConcur

Returns a concur iterable containing the values of concurIterable in iteration order up until but not including the first value for which fn returns a value awaitable to a falsy value.

Example

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, 6, 7, 8, `sloth`]),
takeWhileConcur(value => value < 5),
reduceConcur(toArray()),
),
)
//=> [ 1, 2, 3, 4 ]

window

Returns an iterable containing a rolling window of the values of iterable as arrays of length size.

Throws

if size is not a positive integer.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, 6, `sloth`],
window(3),
reduce(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ] ]

console.log(
pipe(
[1, 2, 3, 4, 5, 6, `sloth`],
window({ size: 3, partialStart: true }),
reduce(toArray()),
),
)
//=> [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ] ]

console.log(
pipe(
[1, 2, 3, 4, 5, 6, `sloth`],
window({ size: 3, partialStart: true, partialEnd: true }),
reduce(toArray()),
),
)
//=> [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ], [ 6, 'sloth' ], [ 'sloth' ] ]

windowAsync

Returns an async iterable containing a rolling window of the values of asyncIterable as arrays of length size.

Throws

if size is not a positive integer.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, `sloth`]),
windowAsync(3),
reduceAsync(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ] ]

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, `sloth`]),
windowAsync({ size: 3, partialStart: true }),
reduceAsync(toArray()),
),
)
//=> [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ] ]

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, `sloth`]),
windowAsync({ size: 3, partialStart: true, partialEnd: true }),
reduceAsync(toArray()),
),
)
//=> [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ], [ 6, 'sloth' ], [ 'sloth' ] ]

windowConcur

Returns a concur iterable containing a rolling window of the values of concurIterable as arrays of length size.

Throws

if size is not a positive integer.

Example

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, 6, `sloth`]),
windowConcur(3),
reduceConcur(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ] ]

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, 6, `sloth`]),
windowConcur({ size: 3, partialStart: true }),
reduceConcur(toArray()),
),
)
//=> [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ] ]

console.log(
await pipe(
asConcur([1, 2, 3, 4, 5, 6, `sloth`]),
windowConcur({ size: 3, partialStart: true, partialEnd: true }),
reduceConcur(toArray()),
),
)
//=> [ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ], [ 4, 5, 6 ], [ 5, 6, 'sloth' ], [ 6, 'sloth' ], [ 'sloth' ] ]

Statistics

Type alias, FunctionDescription

AsyncCompare

A function that compares two values of type Value possibly asynchronously.

A return value that awaits to:

  • Less than zero implies left < right
  • Equal to zero implies left === right
  • Greater than zero implies left > right

Compare

A function that compares two values of type Value.

A return value:

  • Less than zero implies left < right
  • Equal to zero implies left === right
  • Greater than zero implies left > right

MinMax

An object containing a minimum and maximum value.

count

Returns the number of values in iterable.

Like Array.prototype.length, but for iterables.

Example

console.log(count([`sloth`, `more sloth`, `even more sloth`]))
//=> 3

countAsync

Returns a promise that resolves to the number of values in asyncIterable.

Like Array.prototype.length, but for async iterables.

Example

console.log(
await countAsync(asAsync([`sloth`, `more sloth`, `even more sloth`])),
)
//=> 3

countConcur

Returns a promise that resolves to the number of values in concurIterable.

Like Array.prototype.length, but for concur iterables.

Example

console.log(
await countConcur(asConcur([`sloth`, `more sloth`, `even more sloth`])),
)
//=> 3

max

Returns an iterable containing a maximum value of iterable if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(pipe([4, 1, 5, -3], max, get))
//=> 5

maxAsync

Returns an async iterable containing a maximum value of asyncIterable if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(await pipe(asAsync([4, 1, 5, -3]), maxAsync, getAsync))
//=> 5

maxBy

Returns an iterable containing a maximum value of iterable based on the fn Compare function if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(
pipe(
[`eating`, `sleeping`, `yawning`],
maxBy((a, b) => a.length - b.length),
get,
),
)
//=> sleeping

maxByAsync

Returns an async iterable containing a maximum value of asyncIterable based on the fn AsyncCompare function if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(
await pipe(
asAsync([`eating`, `sleeping`, `yawning`]),
maxByAsync((a, b) => a.length - b.length),
getAsync,
),
)
//=> sleeping

maxByConcur

Returns a concur iterable containing a maximum value of concurIterable based on the fn AsyncCompare function if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(
await pipe(
asConcur([`eating`, `sleeping`, `yawning`]),
maxByConcur((a, b) => a.length - b.length),
getConcur,
),
)
//=> sleeping

maxConcur

Returns a concur iterable containing a maximum value of concurIterable if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(await pipe(asConcur([4, 1, 5, -3]), maxConcur, getConcur))
//=> 5

maxWith

Returns an iterable containing a maximum value of iterable by comparing the numerical values of each value, as defined by fn, if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(
pipe(
[`eating`, `sleeping`, `yawning`],
maxWith(value => value.length),
get,
),
)
//=> sleeping

maxWithAsync

Returns an async iterable containing a maximum value of asyncIterable by comparing the numerical values of each value, as defined by fn, if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(
await pipe(
asAsync([`eating`, `sleeping`, `yawning`]),
maxWithAsync(value => value.length),
getAsync,
),
)
//=> sleeping

maxWithConcur

Returns a concur iterable containing a maximum value of concurIterable by comparing the numerical values of each value, as defined by fn, if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(
await pipe(
asConcur([`eating`, `sleeping`, `yawning`]),
maxWithConcur(value => value.length),
getConcur,
),
)
//=> sleeping

mean

Returns the mean of the numbers of iterable.

Returns NaN for an empty iterable.

Example

console.log(mean([1, 4, 6, 2]))
//=> 3.25

console.log(mean([]))
//=> NaN

meanAsync

Returns a promise that resolves to the mean of the numbers of asyncIterable.

Returns a promise that resolves to NaN for an empty async iterable.

Example

console.log(await meanAsync(asAsync([1, 4, 6, 2])))
//=> 3.25

console.log(await meanAsync(emptyAsync))
//=> NaN

meanConcur

Returns a promise that resolves to the mean of the numbers of concurIterable.

Returns a promise that resolves to NaN for an empty concur iterable.

Example

console.log(await meanConcur(asConcur([1, 4, 6, 2])))
//=> 3.25

console.log(await meanConcur(emptyConcur))
//=> NaN

min

Returns an iterable containing a minimum value of iterable if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(pipe([4, 1, 5, -3], min, get))
//=> -3

minAsync

Returns an async iterable containing a minimum value of asyncIterable if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(await pipe(asAsync([4, 1, 5, -3]), minAsync, getAsync))
//=> -3

minBy

Returns an iterable containing a minimum value of iterable based on the fn Compare function if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(
pipe(
[`eating`, `sleeping`, `yawning`],
minBy((a, b) => a.length - b.length),
get,
),
)
//=> eating

minByAsync

Returns an async iterable containing a minimum value of asyncIterable based on the fn AsyncCompare function if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(
await pipe(
asAsync([`eating`, `sleeping`, `yawning`]),
minByAsync((a, b) => a.length - b.length),
getAsync,
),
)
//=> eating

minByConcur

Returns a concur iterable containing a minimum value of concurIterable based on the fn AsyncCompare function if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(
await pipe(
asConcur([`eating`, `sleeping`, `yawning`]),
minByConcur((a, b) => a.length - b.length),
getConcur,
),
)
//=> eating

minConcur

Returns a concur iterable containing a minimum value of concurIterable if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(await pipe(asConcur([4, 1, 5, -3]), minConcur, getConcur))
//=> -3

minMax

Returns an iterable containing a MinMax value of iterable if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(pipe([4, 1, 5, -3], minMax, get))
//=> { min: -3, max: 5 }

minMaxAsync

Returns an async iterable containing a MinMax value of asyncIterable if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(await pipe(asAsync([4, 1, 5, -3]), minMaxAsync, getAsync))
//=> { min: -3, max: 5 }

minMaxBy

Returns an iterable containing a MinMax value of iterable based on the fn Compare function if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(
pipe(
[`eating`, `sleeping`, `yawning`],
minMaxBy((a, b) => a.length - b.length),
get,
),
)
//=> { min: 'eating', max: 'sleeping' }

minMaxByAsync

Returns an async iterable containing a MinMax value of asyncIterable based on the fn AsyncCompare function if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(
await pipe(
asAsync([`eating`, `sleeping`, `yawning`]),
minMaxByAsync((a, b) => a.length - b.length),
getAsync,
),
)
//=> { min: 'eating', max: 'sleeping' }

minMaxByConcur

Returns a concur iterable containing a MinMax value of concurIterable based on the fn AsyncCompare function if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(
await pipe(
asConcur([`eating`, `sleeping`, `yawning`]),
minMaxByConcur((a, b) => a.length - b.length),
getConcur,
),
)
//=> { min: 'eating', max: 'sleeping' }

minMaxConcur

Returns a concur iterable containing a MinMax value of concurIterable if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(await pipe(asConcur([4, 1, 5, -3]), minMaxConcur, getConcur))
//=> { min: -3, max: 5 }

minMaxWith

Returns an iterable containing a MinMax value of iterable by comparing the numerical values of each value, as defined by fn, if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(
pipe(
[`eating`, `sleeping`, `yawning`],
minMaxWith(value => value.length),
get,
),
)
//=> { min: 'eating', max: 'sleeping' }

minMaxWithAsync

Returns an async iterable containing a MinMax value of asyncIterable by comparing the numerical values of each value, as defined by fn, if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(
await pipe(
asAsync([`eating`, `sleeping`, `yawning`]),
minMaxWithAsync(value => value.length),
getAsync,
),
)
//=> { min: 'eating', max: 'sleeping' }

minMaxWithConcur

Returns a concur iterable containing a MinMax value of concurIterable by comparing the numerical values of each value, as defined by fn, if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(
await pipe(
asConcur([`eating`, `sleeping`, `yawning`]),
minMaxWithConcur(value => value.length),
getConcur,
),
)
//=> { min: 'eating', max: 'sleeping' }

minWith

Returns an iterable containing a minimum value of iterable by comparing the numerical values of each value, as defined by fn, if iterable contains at least one value. Otherwise, returns an empty iterable.

Example

console.log(
pipe(
[`eating`, `sleeping`, `yawning`],
minWith(value => value.length),
get,
),
)
//=> eating

minWithAsync

Returns an async iterable containing a minimum value of asyncIterable by comparing the numerical values of each value, as defined by fn, if asyncIterable contains at least one value. Otherwise, returns an empty async iterable.

Example

console.log(
await pipe(
asAsync([`eating`, `sleeping`, `yawning`]),
minWithAsync(value => value.length),
getAsync,
),
)
//=> eating

minWithConcur

Returns a concur iterable containing a minimum value of concurIterable by comparing the numerical values of each value, as defined by fn, if concurIterable contains at least one value. Otherwise, returns an empty concur iterable.

Example

console.log(
await pipe(
asConcur([`eating`, `sleeping`, `yawning`]),
minWithConcur(value => value.length),
getConcur,
),
)
//=> eating

sum

Returns the sum of the numbers of iterable.

Example

console.log(sum([1, 4, 6, 2]))
//=> 13

sumAsync

Returns a promise that resolves to the sum of the numbers of asyncIterable.

Example

console.log(await sumAsync(asAsync([1, 4, 6, 2])))
//=> 3

sumConcur

Returns a promise that resolves to the sum of the numbers of concurIterable.

Example

console.log(await sumConcur(asConcur([1, 4, 6, 2])))
//=> 3

toCount

Returns a Reducer that counts the number of values it receives.

Use when composing reducers. Prefer count, countAsync, and countConcur for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toCount(), toMap())),
),
)
//=> Map(2) { 5 => 2, 10 => 2 }

toMax

Returns an optional reducer that finds the maximum value of the values it receives.

Use when composing reducers. Prefer max for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string.codePointAt(0)]),
reduce(toGrouped(toMax(), toMap())),
),
)
//=> Map(2) { 5 => 115, 10 => 115 }

toMaxBy

Returns an optional reducer that finds the maximum value of the values it receives based on the fn Compare function.

Use when composing reducers. Prefer maxBy for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toMaxBy((s1, s2) => s1.localeCompare(s2)), toMap())),
),
)
//=> Map(2) { 5 => 'sloth', 10 => 'some sloth' }

toMaxByAsync

Returns an async optional reducer that finds the maximum value of the values it receives based on the fn AsyncCompare function.

Use when composing reducers. Prefer maxByAsync and maxByConcur for direct use on iterables.

toMaxWith

Returns an optional reducer that finds the maximum value of the values it receives by comparing the numerical values of each value, as defined by fn.

Use when composing reducers. Prefer maxWith for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toMaxWith(string => string.codePointAt(0)), toMap())),
),
)
//=> Map(2) { 5 => 'sloth', 10 => 'some sloth' }

toMaxWithAsync

Returns an async optional reducer that finds the maximum value of the values it receives by comparing the numerical values of each value, as defined by fn.

Use when composing reducers. Prefer maxWithAsync and maxWithConcur for direct use on iterables.

toMean

Returns a Reducer that computes the mean of the numbers it receives.

Use when composing reducers. Prefer mean, meanAsync, and meanConcur for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, [...string].filter(c => c === `o`).length]),
reduce(toGrouped(toMean(), toMap())),
),
)
//=> Map(2) { 5 => 0.5, 10 => 2 }

toMin

Returns an optional reducer that finds the minimum value of the values it receives.

Use when composing reducers. Prefer min for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string.codePointAt(0)]),
reduce(toGrouped(toMin(), toMap())),
),
)
//=> Map(2) { 5 => 115, 10 => 109 }

toMinBy

Returns an optional reducer that finds the minimum value of the values it receives based on the fn Compare function.

Use when composing reducers. Prefer minBy for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toMinBy((s1, s2) => s1.localeCompare(s2)), toMap())),
),
)
//=> Map(2) { 5 => 'sleep', 10 => 'more sloth' }

toMinByAsync

Returns an async optional reducer that finds the minimum value of the values it receives based on the fn AsyncCompare function.

Use when composing reducers. Prefer minByAsync and minByConcur for direct use on iterables.

toMinMax

Returns an optional reducer that finds the MinMax value of the values it receives.

Use when composing reducers. Prefer minMax for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string.codePointAt(0)]),
reduce(toGrouped(toMinMax(), toMap())),
),
)
//=> Map(2) { 5 => { min: 115, max: 115 }, 10 => { min: 109, max: 115 } }

toMinMaxBy

Returns an optional reducer that finds the MinMax value of the values it receives based on the fn Compare function.

Use when composing reducers. Prefer minMaxBy for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toMinMaxBy((s1, s2) => s1.localeCompare(s2)), toMap())),
),
)
//=> Map(2) {
//=> 5 => { min: 'sleep', max: 'sloth' },
//=> 10 => { min: 'more sloth', max: 'some sloth' }
//=> }

toMinMaxByAsync

Returns an async optional reducer that finds the MinMax value of the values it receives based on the fn AsyncCompare function.

Use when composing reducers. Prefer minMaxByAsync and minMaxByConcur for direct use on iterables.

toMinMaxWith

Returns an optional reducer that finds the MinMax value of the values it receives by comparing the numerical values of each value, as defined by fn.

Use when composing reducers. Prefer minMaxWith for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toMinMaxWith(string => string.codePointAt(0)), toMap())),
),
)
//=> Map(2) {
//=> 5 => { min: 'sloth', max: 'sloth' },
//=> 10 => { min: 'more sloth', max: 'some sloth' }
//=> }

toMinMaxWithAsync

Returns an async optional reducer that finds the MinMax value of the values it receives by comparing the numerical values of each value, as defined by fn.

Use when composing reducers. Prefer minMaxWithAsync and minMaxWithConcur for direct use on iterables.

toMinWith

Returns an optional reducer that finds the minimum value of the values it receives by comparing the numerical values of each value, as defined by fn.

Use when composing reducers. Prefer minWith for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string]),
reduce(toGrouped(toMinWith(string => string.codePointAt(0)), toMap())),
),
)
//=> Map(2) { 5 => 'sloth', 10 => 'more sloth' }

toMinWithAsync

Returns an async optional reducer that finds the minimum value of the values it receives by comparing the numerical values of each value, as defined by fn.

Use when composing reducers. Prefer minWithAsync and minWithConcur for direct use on iterables.

toSum

Returns a Reducer that sums the numbers it receives.

Use when composing reducers. Prefer sum, sumAsync, and sumConcur for direct use on iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `sleep`, `some sloth`],
map(string => [string.length, string.length]),
reduce(toGrouped(toSum(), toMap())),
),
)
//=> Map(2) { 5 => 10, 10 => 20 }

Transforms

FunctionDescription

flatMap

Returns an iterable containing the values of the iterables returned from applying fn to each value of iterable in iteration order.

Like Array.prototype.flatMap, but for iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
flatMap(string => [string, string.length]),
reduce(toArray()),
),
)
//=> [ 'sloth', 5, 'more sloth', 10, 'even more sloth', 15 ]

flatMapAsync

Returns an async iterable containing the values of the async iterables returned, or resolving from promises returned, from applying fn to each value of asyncIterable in iteration order.

Like Array.prototype.flatMap, but for async iterables.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
flatMapAsync(string => [string, string.length]),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 5, 'more sloth', 10, 'even more sloth', 15 ]

flatMapConcur

Returns an concur iterable containing the values of the concur iterables returned, or resolving from promises returned, from applying fn to each value of concurIterable.

Like Array.prototype.flatMap, but for concur iterables.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
flatMapConcur(string => [string, string.length]),
reduceConcur(toArray()),
),
)
//=> [ 'sloth', 5, 'more sloth', 10, 'even more sloth', 15 ]

flatten

Returns an iterable that contains the values of each iterable in iterable in iteration order.

Like Array.prototype.flat, but for iterables.

Example

console.log(
pipe(
[[1, 2], [3, `sloth`, 5], [6, 7]],
flatten,
reduce(toArray()),
),
)
//=> [ 1, 2, 3, 'sloth', 5, 6, 7 ]

flattenAsync

Returns an async iterable that contains the values of each iterable in asyncIterable in iteration order.

Like Array.prototype.flat, but for async iterables.

Example

console.log(
await pipe(
asAsync([asAsync([1, 2]), [3, `sloth`, 5], asAsync([6, 7])]),
flattenAsync,
reduceAsync(toArray()),
),
)
//=> [ 1, 2, 3, 'sloth', 5, 6, 7 ]

flattenConcur

Returns a concur iterable that contains the values of each iterable in concurIterable.

Like Array.prototype.flat, but for concur iterables.

Unlike concat and concatAsync, this function does not necessarily iterate over each iterable in sequence.

Example

console.log(
await pipe(
asConcur([asConcur([1, 2]), [3, `sloth`, 5], asAsync([6, 7])]),
flattenConcur,
reduceConcur(toArray()),
),
)
//=> [ 1, 2, 3, 'sloth', 5, 6, 7 ]

index

Returns an iterable equivalent to iterable except each value of iterable is placed in an entry containing the value's 0-based index in the iteration order followed by the value itself.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
index,
reduce(toArray()),
),
)
//=> [ [ 0, 'sloth' ], [ 1, 'more sloth' ], [ 2, 'even more sloth' ] ]

indexAsync

Returns an async iterable equivalent to asyncIterable except each value of asyncIterable is placed in an entry containing the value's 0-based index in the iteration order followed by the value itself.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
indexAsync,
reduceAsync(toArray()),
),
)
//=> [ [ 0, 'sloth' ], [ 1, 'more sloth' ], [ 2, 'even more sloth' ] ]

indexConcur

Returns a concur iterable equivalent to concurIterable except each value of concurIterable is placed in an entry containing the value's 0-based index in the iteration order followed by the value itself.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
indexConcur,
reduceConcur(toArray()),
),
)
//=> [ [ 0, 'sloth' ], [ 1, 'more sloth' ], [ 2, 'even more sloth' ] ]

map

Returns an iterable containing the values of iterable transformed by fn in iteration order.

Like Array.prototype.map, but for iterables.

Example

console.log(
pipe(
[`sloth`, `more sloth`, `even more sloth`],
map(string => string.length),
reduce(toArray()),
),
)
//=> [ 5, 10, 15 ]

mapAsync

Returns an async iterable containing the values of asyncIterable transformed by fn in iteration order.

Like Array.prototype.map, but for async iterables.

Example

console.log(
await pipe(
asAsync([`sloth`, `more sloth`, `even more sloth`]),
mapAsync(string => string.length),
reduceAsync(toArray()),
),
)
//=> [ 5, 10, 15 ]

mapConcur

Returns a concur iterable containing the values of concurIterable transformed by fn in iteration order.

Like Array.prototype.map, but for concur iterables.

Example

console.log(
await pipe(
asConcur([`sloth`, `more sloth`, `even more sloth`]),
mapConcur(string => string.length),
reduceConcur(toArray()),
),
)
//=> [ 5, 10, 15 ]