Skip to main content

Function: reduce()

Returns the result of reducing iterable using reducer.

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

If reducer is an optional reducer (no RawReducerWithoutFinish.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!

Since

v0.0.1

Call Signature

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

Type Parameters

Value

Acc

Finished

This

Parameters

reducer

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

iterable

Iterable<Value, any, any>

Returns

Finished

Defined in

reducers.d.ts:651

Call Signature

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

Type Parameters

Value

Acc

Finished

This

Parameters

reducer

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

Returns

Function

Parameters

iterable

Iterable<Value, any, any>

Returns

Finished

Defined in

reducers.d.ts:655

Call Signature

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

Type Parameters

Value

Acc

This

Parameters

reducer

Readonly<RawReducerWithoutFinish<Value, Acc, This>>

iterable

Iterable<Value, any, any>

Returns

Acc

Defined in

reducers.d.ts:659

Call Signature

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

Type Parameters

Value

Acc

This

Parameters

reducer

Readonly<RawReducerWithoutFinish<Value, Acc, This>>

Returns

Function

Parameters

iterable

Iterable<Value, any, any>

Returns

Acc

Defined in

reducers.d.ts:663

Call Signature

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

Type Parameters

Value

Finished

This

Parameters

reducer

Readonly<RawOptionalReducerWithFinish<Value, Finished, This>>

iterable

Iterable<Value, any, any>

Returns

Optional<Finished>

Defined in

reducers.d.ts:667

Call Signature

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

Type Parameters

Value

Finished

This

Parameters

reducer

Readonly<RawOptionalReducerWithFinish<Value, Finished, This>>

Returns

Function

Parameters

iterable

Iterable<Value, any, any>

Returns

Optional<Finished>

Defined in

reducers.d.ts:671

Call Signature

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

Type Parameters

Value

This

Parameters

reducer

Readonly<RawOptionalReducerWithoutFinish<Value, This>>

iterable

Iterable<Value, any, any>

Returns

Optional<Value>

Defined in

reducers.d.ts:675

Call Signature

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

Type Parameters

Value

This

Parameters

reducer

Readonly<RawOptionalReducerWithoutFinish<Value, This>>

Returns

Function

Parameters

iterable

Iterable<Value, any, any>

Returns

Optional<Value>

Defined in

reducers.d.ts:679

Call Signature

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

Type Parameters

Value

Parameters

reducer

FunctionReducer<Value>

iterable

Iterable<Value, any, any>

Returns

Optional<Value>

Defined in

reducers.d.ts:683

Call Signature

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

Type Parameters

Value

Parameters

reducer

FunctionReducer<Value>

Returns

Function

Parameters

iterable

Iterable<Value, any, any>

Returns

Optional<Value>

Defined in

reducers.d.ts:687