Skip to main content

Variable: reduce()

const reduce: {<Value, Acc, Finished, This>(reducer, iterable): Finished; <Value, Acc, Finished, This>(reducer): (iterable) => Finished; <Value, Acc, This>(reducer, iterable): Acc; <Value, Acc, This>(reducer): (iterable) => Acc; <Value, Finished, This>(reducer, iterable): Optional<Finished>; <Value, Finished, This>(reducer): (iterable) => Optional<Finished>; <Value, This>(reducer, iterable): Optional<Value>; <Value, This>(reducer): (iterable) => Optional<Value>; <Value>(reducer, iterable): Optional<Value>; <Value>(reducer): (iterable) => Optional<Value>; }

Defined in: reducers.d.ts:650

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.

Call Signature

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

Type Parameters

Value

Value

Acc

Acc

Finished

Finished

This

This

Parameters

reducer

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

iterable

Iterable<Value>

Returns

Finished

Call Signature

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

Type Parameters

Value

Value

Acc

Acc

Finished

Finished

This

This

Parameters

reducer

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

Returns

(iterable): Finished

Parameters

iterable

Iterable<Value>

Returns

Finished

Call Signature

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

Type Parameters

Value

Value

Acc

Acc

This

This

Parameters

reducer

Readonly<RawReducerWithoutFinish<Value, Acc, This>>

iterable

Iterable<Value>

Returns

Acc

Call Signature

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

Type Parameters

Value

Value

Acc

Acc

This

This

Parameters

reducer

Readonly<RawReducerWithoutFinish<Value, Acc, This>>

Returns

(iterable): Acc

Parameters

iterable

Iterable<Value>

Returns

Acc

Call Signature

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

Type Parameters

Value

Value

Finished

Finished

This

This

Parameters

reducer

Readonly<RawOptionalReducerWithFinish<Value, Finished, This>>

iterable

Iterable<Value>

Returns

Optional<Finished>

Call Signature

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

Type Parameters

Value

Value

Finished

Finished

This

This

Parameters

reducer

Readonly<RawOptionalReducerWithFinish<Value, Finished, This>>

Returns

(iterable): Optional<Finished>

Parameters

iterable

Iterable<Value>

Returns

Optional<Finished>

Call Signature

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

Type Parameters

Value

Value

This

This

Parameters

reducer

Readonly<RawOptionalReducerWithoutFinish<Value, This>>

iterable

Iterable<Value>

Returns

Optional<Value>

Call Signature

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

Type Parameters

Value

Value

This

This

Parameters

reducer

Readonly<RawOptionalReducerWithoutFinish<Value, This>>

Returns

(iterable): Optional<Value>

Parameters

iterable

Iterable<Value>

Returns

Optional<Value>

Call Signature

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

Type Parameters

Value

Value

Parameters

reducer

FunctionReducer<Value>

iterable

Iterable<Value>

Returns

Optional<Value>

Call Signature

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

Type Parameters

Value

Value

Parameters

reducer

FunctionReducer<Value>

Returns

(iterable): Optional<Value>

Parameters

iterable

Iterable<Value>

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!

Since

v0.0.1