<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<
Record<K, A>
>Creates a Reducer for combining Records using union, with values for keys that exist in both records combined
using the provided Combiner.
When to use
Use to build a reusable reducer for accumulating many records into one union-shaped record, preserving keys from every input and combining overlapping values with the supplied combiner.
Details
The returned reducer uses Record.union for combine and an empty record as
initialValue, so the default combineAll folds from {} and accumulates
keys from each input record.
export function function makeReducerUnion<
K extends string,
A
>(
combiner: Combiner.Combiner<A>
): Reducer.Reducer<Record<K, A>>
Creates a Reducer for combining Records using union, with values for keys that exist in both records combined
using the provided Combiner.
When to use
Use to build a reusable reducer for accumulating many records into one
union-shaped record, preserving keys from every input and combining
overlapping values with the supplied combiner.
Details
The returned reducer uses Record.union for combine and an empty record as
initialValue, so the default combineAll folds from {} and accumulates
keys from each input record.
makeReducerUnion<function (type parameter) K in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>K extends string, function (type parameter) A in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>A>(combiner: Combiner.Combiner<A>(parameter) combiner: {
combine: (self: A, that: A) => A;
}
combiner: import CombinerCombiner.type Combiner.Combiner = /*unresolved*/ anyCombiner<function (type parameter) A in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, 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<type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<function (type parameter) K in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>K, function (type parameter) A in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>A>> {
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<type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<function (type parameter) K in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>K, function (type parameter) A in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>A>>(
(self: Record<K, A>self, that: Record<K, A>that) => const union: {
<K1 extends string, A, B, C>(
that: ReadonlyRecord<K1, B>,
combine: (selfValue: A, thatValue: B) => C
): <K0 extends string>(
self: ReadonlyRecord<K0, A>
) => Record<K0 | K1, A | B | C>
<K0 extends string, A, K1 extends string, B, C>(
self: ReadonlyRecord<K0, A>,
that: ReadonlyRecord<K1, B>,
combine: (selfValue: A, thatValue: B) => C
): Record<K0 | K1, A | B | C>
}
union(self: Record<K, A>self, that: Record<K, A>that, combiner: Combiner.Combiner<A>(parameter) combiner: {
combine: (self: A, that: A) => A;
}
combiner.combine),
{} as type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<function (type parameter) K in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>K, function (type parameter) A in makeReducerUnion<K extends string, A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Record<K, A>>A>
)
}