<N extends Newtype.Any>(
reducer: Reducer.Reducer<Newtype.Carrier<N>>
): Reducer.Reducer<N>Lifts a Reducer for the carrier type into a Reducer for the newtype.
When to use
Use when you need to reduce a collection of newtype-wrapped values with the carrier's reducer, without manually unwrapping.
Details
The returned reducer delegates to the provided carrier reducer.
Example (Reducing newtypes)
import { Newtype, Reducer } from "effect"
interface Score extends Newtype.Newtype<"Score", number> {}
const sum = Reducer.make<number>((a, b) => a + b, 0)
const reducer = Newtype.makeReducer<Score>(sum)
const iso = Newtype.makeIso<Score>()
const total = reducer.combineAll([iso.set(1), iso.set(2), iso.set(3)])
Newtype.value(total) // 6export const const makeReducer: <
N extends Newtype.Any
>(
reducer: Reducer.Reducer<Newtype.Carrier<N>>
) => Reducer.Reducer<N>
Lifts a Reducer for the carrier type into a Reducer for the newtype.
When to use
Use when you need to reduce a collection of newtype-wrapped values with the
carrier's reducer, without manually unwrapping.
Details
The returned reducer delegates to the provided carrier reducer.
Example (Reducing newtypes)
import { Newtype, Reducer } from "effect"
interface Score extends Newtype.Newtype<"Score", number> {}
const sum = Reducer.make<number>((a, b) => a + b, 0)
const reducer = Newtype.makeReducer<Score>(sum)
const iso = Newtype.makeIso<Score>()
const total = reducer.combineAll([iso.set(1), iso.set(2), iso.set(3)])
Newtype.value(total) // 6
makeReducer: <function (type parameter) N in <N extends Newtype.Any>(reducer: Reducer.Reducer<Newtype.Carrier<N>>): Reducer.Reducer<N>N extends Newtype.type Newtype<in out Key extends string, out Carrier>.Any = Newtype<any, unknown>A type that matches any Newtype, useful as a generic constraint:
<N extends Newtype.Any>.
When to use
Use as a generic constraint when a type parameter can be any Newtype.
Any>(reducer: Reducer.Reducer<Newtype.Carrier<N>>(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<Newtype.type Newtype<in out Key extends string, out Carrier>.Carrier<N extends Newtype.Any> = N extends Newtype<infer _Key extends string, infer Carrier> ? Carrier : neverExtracts the carrier (underlying) type from a newtype.
When to use
Use when you need to refer to the wrapped type in generic utilities.
Carrier<function (type parameter) N in <N extends Newtype.Any>(reducer: Reducer.Reducer<Newtype.Carrier<N>>): Reducer.Reducer<N>N>>) => 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) N in <N extends Newtype.Any>(reducer: Reducer.Reducer<Newtype.Carrier<N>>): Reducer.Reducer<N>N> =
import castcast