<A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<A | undefined>Creates a Reducer for UndefinedOr<A> that prioritizes the first non-undefined
value and combines values when both operands are present.
When to use
Use when you need to reduce values that may be undefined, keeping the
first defined value as a fallback and combining only when both operands are
defined.
Details
Combining undefined with undefined returns undefined. Combining a
defined value with undefined keeps the defined value, so the first defined
value wins when only one side is present. When both values are defined, they
are combined with combiner.combine. The reducer's initial value is
undefined.
export function function makeReducer<A>(
combiner: Combiner.Combiner<A>
): Reducer.Reducer<A | undefined>
Creates a Reducer for UndefinedOr<A> that prioritizes the first non-undefined
value and combines values when both operands are present.
When to use
Use when you need to reduce values that may be undefined, keeping the
first defined value as a fallback and combining only when both operands are
defined.
Details
Combining undefined with undefined returns undefined. Combining a
defined value with undefined keeps the defined value, so the first defined
value wins when only one side is present. When both values are defined, they
are combined with combiner.combine. The reducer's initial value is
undefined.
makeReducer<function (type parameter) A in makeReducer<A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<A | undefined>A>(combiner: Combiner.Combiner<A>(parameter) combiner: {
combine: (self: A, that: A) => A;
}
combiner: import CombinerCombiner.interface Combiner<A>Represents a strategy for combining two values of the same type A. A
Combiner contains a single combine method that takes two values and
returns a merged result. It does not include an identity/empty value; use
Reducer when you need one.
When to use
Use when you need to describe how two values of the same type
merge, pass a reusable combining strategy to library functions like
Struct.makeCombiner or Option.makeCombinerFailFast, or define the
combining step for a Reducer.
Example (Combining numbers with addition)
import { Combiner } from "effect"
const Sum = Combiner.make<number>((self, that) => self + that)
console.log(Sum.combine(3, 4))
// Output: 7
Combiner<function (type parameter) A in makeReducer<A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<A | undefined>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>(combiner: Combiner.Combiner<A>): Reducer.Reducer<A | undefined>A | undefined> {
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((self: A | undefinedself, that: A | undefinedthat) => {
if (self: A | undefinedself === var undefinedundefined) return that: A | undefinedthat
if (that: A | undefinedthat === var undefinedundefined) return self: A & ({} | null)self
return combiner: Combiner.Combiner<A>(parameter) combiner: {
combine: (self: A, that: A) => A;
}
combiner.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(self: A & ({} | null)self, that: A & ({} | null)that)
}, var undefinedundefined as function (type parameter) A in makeReducer<A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<A | undefined>A | undefined)
}