Skip to main content

Function: 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!

reduce(reducer, iterable)

reduce<Value, Acc, Finished, This>(reducer, iterable): Finished

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.

Type Parameters

Value

Acc

Finished

This

Parameters

reducer: Readonly<RawReducerWithFinish<Value, Acc, Finished, This>>

iterable: Iterable<Value, any, any>

Returns

Finished

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!

Defined in

reducers.d.ts:483

reduce(reducer)

reduce<Value, Acc, Finished, This>(reducer): (iterable) => Finished

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.

Type Parameters

Value

Acc

Finished

This

Parameters

reducer: Readonly<RawReducerWithFinish<Value, Acc, Finished, This>>

Returns

Function

Parameters

iterable: Iterable<Value, any, any>

Returns

Finished

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!

Defined in

reducers.d.ts:487

reduce(reducer, iterable)

reduce<Value, Acc, This>(reducer, iterable): Acc

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.

Type Parameters

Value

Acc

This

Parameters

reducer: Readonly<RawReducerWithoutFinish<Value, Acc, This>>

iterable: Iterable<Value, any, any>

Returns

Acc

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!

Defined in

reducers.d.ts:491

reduce(reducer)

reduce<Value, Acc, This>(reducer): (iterable) => Acc

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.

Type Parameters

Value

Acc

This

Parameters

reducer: Readonly<RawReducerWithoutFinish<Value, Acc, This>>

Returns

Function

Parameters

iterable: Iterable<Value, any, any>

Returns

Acc

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!

Defined in

reducers.d.ts:495

reduce(reducer, iterable)

reduce<Value, Finished, This>(reducer, iterable): Optional<Finished>

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.

Type Parameters

Value

Finished

This

Parameters

reducer: Readonly<RawOptionalReducerWithFinish<Value, Finished, This>>

iterable: Iterable<Value, any, any>

Returns

Optional<Finished>

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!

Defined in

reducers.d.ts:499

reduce(reducer)

reduce<Value, Finished, This>(reducer): (iterable) => Optional<Finished>

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.

Type Parameters

Value

Finished

This

Parameters

reducer: Readonly<RawOptionalReducerWithFinish<Value, Finished, This>>

Returns

Function

Parameters

iterable: Iterable<Value, any, any>

Returns

Optional<Finished>

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!

Defined in

reducers.d.ts:503

reduce(reducer, iterable)

reduce<Value, This>(reducer, iterable): Optional<Value>

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.

Type Parameters

Value

This

Parameters

reducer: Readonly<RawOptionalReducerWithoutFinish<Value, This>>

iterable: Iterable<Value, any, any>

Returns

Optional<Value>

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!

Defined in

reducers.d.ts:507

reduce(reducer)

reduce<Value, This>(reducer): (iterable) => Optional<Value>

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.

Type Parameters

Value

This

Parameters

reducer: Readonly<RawOptionalReducerWithoutFinish<Value, This>>

Returns

Function

Parameters

iterable: Iterable<Value, any, any>

Returns

Optional<Value>

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!

Defined in

reducers.d.ts:511

reduce(reducer, iterable)

reduce<Value>(reducer, iterable): Optional<Value>

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.

Type Parameters

Value

Parameters

reducer: FunctionReducer<Value>

iterable: Iterable<Value, any, any>

Returns

Optional<Value>

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!

Defined in

reducers.d.ts:515

reduce(reducer)

reduce<Value>(reducer): (iterable) => Optional<Value>

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.

Type Parameters

Value

Parameters

reducer: FunctionReducer<Value>

Returns

Function

Parameters

iterable: Iterable<Value, any, any>

Returns

Optional<Value>

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!

Defined in

reducers.d.ts:519