<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): BFolds 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)) // 6export 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
}
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
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, <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B) => B>(arity: 3, body: <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B) => B): ((...args: Array<any>) => any) & (<B, A>(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) B in <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB, function (type parameter) A in <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA>, b: Bb: function (type parameter) B in <B, A>(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 <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB, a: Aa: function (type parameter) A in <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BA, i: numberi: number) => function (type parameter) B in <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB): function (type parameter) B in <B, A>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): BB =>
const fromIterable: <A>(
collection: Iterable<A>
) => A[]
Converts an Iterable to an Array.
When to use
Use to convert any Iterable (Set, Generator, etc.) into an array.
Details
If the input is already an array, this returns it by reference without
copying. Otherwise, it creates a new array from the iterable. Use copy if
you need a fresh array even when the input is already an array.
Example (Converting a Set to an array)
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result) // [1, 2, 3]
fromIterable(self: Iterable<A>self).Array<A>.reduce<B>(callbackfn: (previousValue: B, currentValue: A, currentIndex: number, array: A[]) => B, initialValue: B): B (+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((b: Bb, a: Aa, i: numberi) => f: (b: B, a: A, i: number) => Bf(b: Bb, a: Aa, i: numberi), b: Bb)
)