<A extends ReadonlyArray<unknown>>(reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}): Reducer.Reducer<A>Creates a Reducer for a tuple shape by providing a Reducer for each
position. The initial value is derived from each position's
Reducer.initialValue. When reducing a collection of tuples, each element
is combined independently.
When to use
Use when you need to fold same-shape tuples by accumulating each position independently into one summary tuple.
Example (Reducing a collection of tuples)
import { Number, String, Tuple } from "effect"
const R = Tuple.makeReducer<readonly [number, string]>([
Number.ReducerSum,
String.ReducerConcat
])
const result = R.combineAll([
[1, "a"],
[2, "b"],
[3, "c"]
])
console.log(result) // [6, "abc"]export function function makeReducer<
A extends ReadonlyArray<unknown>
>(reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}): Reducer.Reducer<A>
Creates a Reducer for a tuple shape by providing a Reducer for each
position. The initial value is derived from each position's
Reducer.initialValue. When reducing a collection of tuples, each element
is combined independently.
When to use
Use when you need to fold same-shape tuples by accumulating each position
independently into one summary tuple.
Example (Reducing a collection of tuples)
import { Number, String, Tuple } from "effect"
const R = Tuple.makeReducer<readonly [number, string]>([
Number.ReducerSum,
String.ReducerConcat
])
const result = R.combineAll([
[1, "a"],
[2, "b"],
[3, "c"]
])
console.log(result) // [6, "abc"]
makeReducer<function (type parameter) A in makeReducer<A extends ReadonlyArray<unknown>>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }): Reducer.Reducer<A>A extends interface ReadonlyArray<T>ReadonlyArray<unknown>>(
reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers: { readonly [function (type parameter) KK in keyof function (type parameter) A in makeReducer<A extends ReadonlyArray<unknown>>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }): Reducer.Reducer<A>A]: import ReducerReducer.interface Reducer<A>Represents a strategy for reducing a collection of values of type A into
a single result.
When to use
Use when you need to fold/reduce a collection into a single value.
- You want a reusable reducing strategy that can be passed to library
functions like
Struct.makeReducer, Option.makeReducer, or
Record.makeReducerUnion.
- You need both the combining logic and a known starting value.
Details
Extends
Combiner.Combiner
with:
initialValue – the identity/neutral element for combine.
combineAll – folds an entire Iterable<A> from initialValue.
Many modules ship pre-built reducers:
Number.ReducerSum, Number.ReducerMultiply
String.ReducerConcat
Boolean.ReducerAnd, Boolean.ReducerOr
Example (String concatenation reducer)
import { Reducer } from "effect"
const Concat = Reducer.make<string>((a, b) => a + b, "")
console.log(Concat.combineAll(["hello", " ", "world"]))
// Output: "hello world"
Reducer<function (type parameter) A in makeReducer<A extends ReadonlyArray<unknown>>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }): Reducer.Reducer<A>A[function (type parameter) KK]> }
): import ReducerReducer.interface Reducer<A>Represents a strategy for reducing a collection of values of type A into
a single result.
When to use
Use when you need to fold/reduce a collection into a single value.
- You want a reusable reducing strategy that can be passed to library
functions like
Struct.makeReducer, Option.makeReducer, or
Record.makeReducerUnion.
- You need both the combining logic and a known starting value.
Details
Extends
Combiner.Combiner
with:
initialValue – the identity/neutral element for combine.
combineAll – folds an entire Iterable<A> from initialValue.
Many modules ship pre-built reducers:
Number.ReducerSum, Number.ReducerMultiply
String.ReducerConcat
Boolean.ReducerAnd, Boolean.ReducerOr
Example (String concatenation reducer)
import { Reducer } from "effect"
const Concat = Reducer.make<string>((a, b) => a + b, "")
console.log(Concat.combineAll(["hello", " ", "world"]))
// Output: "hello world"
Reducer<function (type parameter) A in makeReducer<A extends ReadonlyArray<unknown>>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }): Reducer.Reducer<A>A> {
const const combine: (self: A, that: A) => Acombine = function makeCombiner<
A extends ReadonlyArray<unknown>
>(combiners: {
readonly [K in keyof A]: Combiner.Combiner<A[K]>
}): Combiner.Combiner<A>
Creates a Combiner for a tuple shape by providing a Combiner for each
position. When two tuples are combined, each element is merged using its
corresponding combiner.
When to use
Use when you need to merge two same-shape tuples by combining each position
independently, such as summing counters or concatenating strings.
Example (Combining tuple elements)
import { Number, String, Tuple } from "effect"
const C = Tuple.makeCombiner<readonly [number, string]>([
Number.ReducerSum,
String.ReducerConcat
])
const result = C.combine([1, "hello"], [2, " world"])
console.log(result) // [3, "hello world"]
makeCombiner(reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers).Combiner<A>.combine: (self: A, that: A) => ACombines two values into a new value.
When to use
Use to merge two values according to this combining strategy.
combine
const const initialValue: any[]initialValue = []
for (let let i: numberi = 0; let i: numberi < reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers.ReadonlyArray<T>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length; let i: numberi++) {
const initialValue: any[]initialValue.Array<any>.push(...items: any[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers[let i: numberi].Reducer<unknown>.initialValue: unknownNeutral starting value (combining with this changes nothing).
When to use
Use to seed a reduction and represent the result of reducing an empty collection.
initialValue)
}
return import ReducerReducer.function make<A>(
combine: (self: A, that: A) => A,
initialValue: A,
combineAll?: (collection: Iterable<A>) => A
): Reducer<A>
Creates a Reducer from a combine function and an initialValue.
When to use
Use when you have a custom reducing operation not covered by a pre-built reducer.
- You want to provide an optimized
combineAll (e.g. short-circuiting on
a known absorbing element like 0 for multiplication).
Details
- If
combineAll is omitted, a default left-to-right fold starting from
initialValue is used.
- If
combineAll is provided, it completely replaces the default fold.
Example (Multiplying with short-circuit)
import { Reducer } from "effect"
const Product = Reducer.make<number>(
(a, b) => a * b,
1,
(collection) => {
let acc = 1
for (const n of collection) {
if (n === 0) return 0
acc *= n
}
return acc
}
)
console.log(Product.combineAll([2, 3, 4]))
// Output: 24
console.log(Product.combineAll([2, 0, 4]))
// Output: 0
make(const combine: (self: A, that: A) => Acombine, const initialValue: unknown[]initialValue as unknown as function (type parameter) A in makeReducer<A extends ReadonlyArray<unknown>>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }): Reducer.Reducer<A>A)
}