<A>(
reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]> },
options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined
}
): Reducer.Reducer<A>Creates a Reducer for a struct shape by providing a Reducer for each
property. The initial value is derived from each property's
Reducer.initialValue. When reducing a collection of structs, each property
is combined independently.
When to use
Use when you need to fold same-shape records by accumulating each property independently into one summary record.
Details
Pass omitKeyWhen to drop properties whose reduced value matches a
predicate.
Example (Reducing a collection of structs)
import { Number, String, Struct } from "effect"
const R = Struct.makeReducer<{ readonly n: number; readonly s: string }>({
n: Number.ReducerSum,
s: String.ReducerConcat
})
const result = R.combineAll([
{ n: 1, s: "a" },
{ n: 2, s: "b" },
{ n: 3, s: "c" }
])
console.log(result) // { n: 6, s: "abc" }export function function makeReducer<A>(
reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
},
options?: {
readonly omitKeyWhen?:
| ((a: A[keyof A]) => boolean)
| undefined
}
): Reducer.Reducer<A>
Creates a Reducer for a struct shape by providing a Reducer for each
property. The initial value is derived from each property's
Reducer.initialValue. When reducing a collection of structs, each property
is combined independently.
When to use
Use when you need to fold same-shape records by accumulating each property
independently into one summary record.
Details
Pass omitKeyWhen to drop properties whose reduced value matches a
predicate.
Example (Reducing a collection of structs)
import { Number, String, Struct } from "effect"
const R = Struct.makeReducer<{ readonly n: number; readonly s: string }>({
n: Number.ReducerSum,
s: String.ReducerConcat
})
const result = R.combineAll([
{ n: 1, s: "a" },
{ n: 2, s: "b" },
{ n: 3, s: "c" }
])
console.log(result) // { n: 6, s: "abc" }
makeReducer<function (type parameter) A in makeReducer<A>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A>(
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>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): 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>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A[function (type parameter) KK]> },
options: | {
readonly omitKeyWhen?:
| ((a: A[keyof A]) => boolean)
| undefined
}
| undefined
options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefinedomitKeyWhen?: ((a: A[keyof A]a: function (type parameter) A in makeReducer<A>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A[keyof function (type parameter) A in makeReducer<A>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A]) => boolean) | undefined
}
): 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>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A> {
const const combine: (self: A, that: A) => Acombine = function makeCombiner<A>(
combiners: {
readonly [K in keyof A]: Combiner.Combiner<
A[K]
>
},
options?: {
readonly omitKeyWhen?:
| ((a: A[keyof A]) => boolean)
| undefined
}
): Combiner.Combiner<A>
Creates a Combiner for a struct shape by providing a Combiner for each
property. When two structs are combined, each property is merged using its
corresponding combiner.
When to use
Use when you need to merge two same-shape records by combining each property
independently, such as summing counters or concatenating strings.
Details
Pass omitKeyWhen to drop properties whose merged value matches a predicate,
such as omitting zero counters.
Example (Combining struct properties)
import { Number, String, Struct } from "effect"
const C = Struct.makeCombiner<{ readonly n: number; readonly s: string }>({
n: Number.ReducerSum,
s: String.ReducerConcat
})
const result = C.combine({ n: 1, s: "hello" }, { n: 2, s: " world" })
console.log(result) // { n: 3, s: "hello world" }
makeCombiner(reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers, options: | {
readonly omitKeyWhen?:
| ((a: A[keyof A]) => boolean)
| undefined
}
| undefined
options).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: AinitialValue = {} as function (type parameter) A in makeReducer<A>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A
for (const const key: keyof Akey of Reflect.function Reflect.ownKeys(target: object): (string | symbol)[]Returns the string and symbol keys of the own properties of an object. The own properties of an object
are those that are defined directly on that object, and are not inherited from the object's prototype.
ownKeys(reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers) as interface Array<T>Array<keyof function (type parameter) A in makeReducer<A>(reducers: { readonly [K in keyof A]: Reducer.Reducer<A[K]>; }, options?: {
readonly omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefined;
}): Reducer.Reducer<A>
A>) {
const const iv: A[keyof A]iv = reducers: {
readonly [K in keyof A]: Reducer.Reducer<A[K]>
}
reducers[const key: keyof Akey].Reducer<A[keyof A]>.initialValue: A[keyof A]Neutral 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
if (options: | {
readonly omitKeyWhen?:
| ((a: A[keyof A]) => boolean)
| undefined
}
| undefined
options?.omitKeyWhen?: ((a: A[keyof A]) => boolean) | undefinedomitKeyWhen?.(const iv: A[keyof A]iv)) continue
const initialValue: AinitialValue[const key: keyof Akey] = const iv: A[keyof A]iv
}
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: AinitialValue)
}