<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
} = dual<(...args: Array<any>) => any, <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B) => B>(arity: 3, body: <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B) => B): ((...args: Array<any>) => any) & (<A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B) => B) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(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
})