Hyperlinkv0.8.0-beta.28

Array

Array.reduceconsteffect/Array.ts:3893
<B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => B
<A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B

Folds an iterable from left to right into a single value.

When to use

Use to combine all elements into one accumulated value from left to right.

Details

The function receives (accumulator, element, index).

Example (Summing an array)

import { Array } from "effect"

console.log(Array.reduce([1, 2, 3], 0, (acc, n) => acc + n)) // 6
Source effect/Array.ts:38938 lines
export const reduce: {
  <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => B
  <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B
} = dual(
  3,
  <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B =>
    fromIterable(self).reduce((b, a, i) => f(b, a, i), b)
)
Referenced by 1 symbols