<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<Option<A>>Creates a Reducer for Option<A> by lifting an existing Reducer with
fail-fast semantics.
When to use
Use when you need to reduce Option values with fail-fast semantics, where
any None aborts the entire result instead of being skipped.
Details
- Initial value is
Some(reducer.initialValue) - Combines only when both operands are
Some - Any
Nonecauses the result to becomeNoneimmediately
Example (Fail-fast reducing)
import { Number, Option } from "effect"
const reducer = Option.makeReducerFailFast(Number.ReducerSum)
console.log(reducer.combineAll([Option.some(1), Option.some(2)]))
// Output: { _id: 'Option', _tag: 'Some', value: 3 }
console.log(reducer.combineAll([Option.some(1), Option.none()]))
// Output: { _id: 'Option', _tag: 'None' }export function function makeReducerFailFast<A>(
reducer: Reducer.Reducer<A>
): Reducer.Reducer<Option<A>>
Creates a Reducer for Option<A> by lifting an existing Reducer with
fail-fast semantics.
When to use
Use when you need to reduce Option values with fail-fast semantics, where
any None aborts the entire result instead of being skipped.
Details
- Initial value is
Some(reducer.initialValue)
- Combines only when both operands are
Some
- Any
None causes the result to become None immediately
Example (Fail-fast reducing)
import { Number, Option } from "effect"
const reducer = Option.makeReducerFailFast(Number.ReducerSum)
console.log(reducer.combineAll([Option.some(1), Option.some(2)]))
// Output: { _id: 'Option', _tag: 'Some', value: 3 }
console.log(reducer.combineAll([Option.some(1), Option.none()]))
// Output: { _id: 'Option', _tag: 'None' }
makeReducerFailFast<function (type parameter) A in makeReducerFailFast<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<Option<A>>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<Option<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 Option<A> = None<A> | Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in makeReducerFailFast<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<Option<A>>A>> {
const const combine: anycombine = function makeCombinerFailFast<A>(
combiner: Combiner.Combiner<A>
): Combiner.Combiner<Option<A>>
Creates a Combiner for Option<A> with fail-fast semantics: returns None
if either operand is None.
When to use
Use when you need an Option combiner that returns None unless both
operands are Some.
Details
None + anything → None
- anything +
None → None
Some(a) + Some(b) → Some(combine(a, b))
Example (Fail-fast combining)
import { Number, Option } from "effect"
const combiner = Option.makeCombinerFailFast(Number.ReducerSum)
console.log(combiner.combine(Option.some(1), Option.some(2)))
// Output: { _id: 'Option', _tag: 'Some', value: 3 }
console.log(combiner.combine(Option.some(1), Option.none()))
// Output: { _id: 'Option', _tag: 'None' }
makeCombinerFailFast(reducer: Reducer.Reducer<A>(parameter) reducer: {
initialValue: A;
combineAll: (collection: Iterable<A>) => A;
combine: (self: A, that: A) => A;
}
reducer).combine
const const initialValue: Option<A>initialValue = const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(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)
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: anycombine, const initialValue: Option<A>initialValue, (collection: Iterable<Option<A>>collection) => {
let let out: Option<A>out = const initialValue: Option<A>initialValue
for (const const value: Option<A>value of collection: Iterable<Option<A>>collection) {
let out: Option<A>out = const combine: anycombine(let out: Option<A>out, const value: Option<A>value)
if (const isNone: <A>(
self: Option<A>
) => self is None<A>
Checks whether an Option is None (absent).
When to use
Use when you need to branch on an absent Option before accessing .value.
Details
- Acts as a type guard, narrowing to
None<A>
Example (Checking for None)
import { Option } from "effect"
console.log(Option.isNone(Option.some(1)))
// Output: false
console.log(Option.isNone(Option.none()))
// Output: true
isNone(let out: Option<A>out)) return let out: None<A>let out: {
_tag: "None";
_op: "None";
valueOrUndefined: undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
out
}
return let out: Option<A>out
})
}