Hyperlinkv0.8.0-beta.28

Iterable

Iterable.reduceconsteffect/Iterable.ts:2215
<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

Reduces an iterable to a single value by applying a function to each element and accumulating the result.

Details

This function applies a reducing function against an accumulator and each element of the iterable (from left to right) to reduce it to a single value.

Example (Reducing an iterable)

import { Iterable } from "effect"

// Sum all numbers
const numbers = [1, 2, 3, 4, 5]
const sum = Iterable.reduce(numbers, 0, (acc, n) => acc + n)
console.log(sum) // 15

// Find maximum value
const values = [3, 1, 4, 1, 5, 9, 2]
const max = Iterable.reduce(values, -Infinity, Math.max)
console.log(max) // 9

// Build an object from key-value pairs
const pairs = [["a", 1], ["b", 2], ["c", 3]] as const
const obj = Iterable.reduce(
  pairs,
  {} as Record<string, number>,
  (acc, [key, value]) => {
    acc[key] = value
    return acc
  }
)
console.log(obj) // { a: 1, b: 2, c: 3 }

// Use index in the reducer
const letters = ["a", "b", "c"]
const indexed = Iterable.reduce(
  letters,
  [] as Array<string>,
  (acc, letter, i) => {
    acc.push(`${i}: ${letter}`)
    return acc
  }
)
console.log(indexed) // ["0: a", "1: b", "2: c"]
folding
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, <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B => {
  if (Array.isArray(self)) {
    return self.reduce(f, b)
  }
  let i = 0
  let result = b
  for (const n of self) {
    result = f(result, n, i++)
  }
  return result
})