Reducer
Reusable strategies for reducing many values into one value. A Reducer<A>
extends Combiner.Combiner with an initialValue for empty collections and
a combineAll method for folding an entire iterable. This module provides a
constructor for reducers and a helper for reversing the order in which values
are combined.
Combinators
Reverses the argument order of a reducer's combine method.
When to use
Use when you want the right-hand value to act as the accumulator, or need to reverse a non-commutative reducer such as string concatenation.
Details
- Returns a new
Reducerwherecombine(self, that)calls the original reducer ascombine(that, self). - The
initialValueis preserved from the original reducer. - The
combineAllis re-derived from the flippedcombine(using the default left-to-right fold), not carried over from the original.
See
- make
- Combiner.flip – the same operation on a plain
Combiner
Signature
declare function flip<A>(reducer: Reducer<A>): Reducer<A>Example
(Reversing string concatenation)
import { Reducer, String } from "effect"
const Prepend = Reducer.flip(String.ReducerConcat)
Prepend.combine("a", "b") // => "ba"Prepend.combineAll(["a", "b", "c"]) // => "cba"Constructors
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 like0for multiplication).
Details
- If
combineAllis omitted, a default left-to-right fold starting frominitialValueis used. - If
combineAllis provided, it completely replaces the default fold.
See
Signature
declare function make<A>(combine: (self: A, that: A) => A, initialValue: A, combineAll?: (collection: Iterable<A>) => A): Reducer<A>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 })
Product.combineAll([2, 3, 4]) // => 24Product.combineAll([2, 0, 4]) // => 0Models
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, orRecord.makeReducerUnion. - You need both the combining logic and a known starting value.
Details
Extends Combiner.Combiner with:
initialValue– the identity/neutral element forcombine.combineAll– folds an entireIterable<A>frominitialValue.
Many modules ship pre-built reducers:
Number.ReducerSum,Number.ReducerMultiplyString.ReducerConcatBoolean.ReducerAnd,Boolean.ReducerOr
See
- make – create a
Reducerfrom a function and initial value - Combiner.Combiner – parent interface without
initialValue
Signature
interface Reducer<A> extends Combiner<A> { readonly combineAll: (collection: Iterable<A>) => A; readonly initialValue: A;}Example
(String concatenation reducer)
import { Reducer } from "effect"
const Concat = Reducer.make<string>((a, b) => a + b, "")
Concat.combineAll(["hello", " ", "world"]) // => "hello world"