<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): BReduces 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"]export const 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
}
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"]
reduce: {
<function (type parameter) B in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BB, function (type parameter) A in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BA>(b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BB, f: (b: B, a: A, i: number) => Bf: (b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BB, a: Aa: function (type parameter) A in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BA, i: numberi: number) => function (type parameter) B in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BB): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BA>) => function (type parameter) B in <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => BB
<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA, function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA>, b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB, f: (b: B, a: A, i: number) => Bf: (b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB, a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA, i: numberi: number) => function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB): function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB
} = import dualdual(3, <function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA, function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA>, b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB, f: (b: B, a: A, i: number) => Bf: (b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB, a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA, i: numberi: number) => function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB): function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB => {
if (var Array: ArrayConstructorArray.ArrayConstructor.isArray(arg: any): arg is any[]isArray(self: Iterable<A>self)) {
return self: any[]self.Array<any>.reduce(callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any (+2 overloads)Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
reduce(f: (b: B, a: A, i: number) => Bf, b: Bb)
}
let let i: numberi = 0
let let result: Bresult = b: Bb
for (const const n: An of self: Iterable<A>self) {
let result: Bresult = f: (b: B, a: A, i: number) => Bf(let result: Bresult, const n: An, let i: numberi++)
}
return let result: Bresult
})