<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<A | undefined>Creates a Reducer for A | undefined by wrapping an existing reducer with
fail-fast semantics.
When to use
Use to wrap an existing Reducer so any undefined value aborts the entire
reduction result.
Details
- Initial value is the wrapped reducer's
initialValue - Combining two defined values delegates to the wrapped reducer
- If the accumulator or next value is
undefined, the reduction returnsundefined
export function function makeReducerFailFast<A>(
reducer: Reducer.Reducer<A>
): Reducer.Reducer<A | undefined>
Creates a Reducer for A | undefined by wrapping an existing reducer with
fail-fast semantics.
When to use
Use to wrap an existing Reducer so any undefined value aborts the entire
reduction result.
Details
- Initial value is the wrapped reducer's
initialValue
- Combining two defined values delegates to the wrapped reducer
- If the accumulator or next value is
undefined, the reduction returns undefined
makeReducerFailFast<function (type parameter) A in makeReducerFailFast<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<A | undefined>A>(reducer: Reducer.Reducer<A>(parameter) reducer: {
initialValue: A;
combineAll: (collection: Iterable<A>) => A;
combine: (self: A, that: A) => A;
}
reducer: 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 makeReducerFailFast<A>(reducer: Reducer.Reducer<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 makeReducerFailFast<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<A | undefined>A | undefined> {
const const combine: (
self: A | undefined,
that: A | undefined
) => A | undefined
combine = function makeCombinerFailFast<A>(
combiner: Combiner.Combiner<A>
): Combiner.Combiner<A | undefined>
Creates a Combiner for A | undefined that combines values only when both
operands are defined.
When to use
Use to lift a Combiner so any undefined operand makes the combined result
undefined.
Details
undefined combined with any value returns undefined
- Any value combined with
undefined returns undefined
a combined with b returns combiner.combine(a, b)
makeCombinerFailFast(reducer: Reducer.Reducer<A>(parameter) reducer: {
initialValue: A;
combineAll: (collection: Iterable<A>) => A;
combine: (self: A, that: A) => A;
}
reducer).Combiner<A | undefined>.combine: (self: A | undefined, that: A | undefined) => A | undefinedCombines two values into a new value.
When to use
Use to merge two values according to this combining strategy.
combine
const const initialValue: A | undefinedinitialValue = reducer: Reducer.Reducer<A>(parameter) reducer: {
initialValue: A;
combineAll: (collection: Iterable<A>) => A;
combine: (self: A, that: A) => A;
}
reducer.Reducer<A>.initialValue: ANeutral 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 as function (type parameter) A in makeReducerFailFast<A>(reducer: Reducer.Reducer<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(const combine: (
self: A | undefined,
that: A | undefined
) => A | undefined
combine, const initialValue: A | undefinedinitialValue, (collection: Iterable<A | undefined>collection) => {
let let out: A | undefinedout = const initialValue: A | undefinedinitialValue
for (const const value: A | undefinedvalue of collection: Iterable<A | undefined>collection) {
let out: A | undefinedout = const combine: (
self: A | undefined,
that: A | undefined
) => A | undefined
combine(let out: A | undefinedout, const value: A | undefinedvalue)
if (let out: A | undefinedout === var undefinedundefined) return let out: A | undefinedout
}
return let out: A | undefinedout
})
}