Reducer_.Reducer<Ordering>Reducer for combining Orderings.
When to use
Use to combine multiple comparison results in priority order, such as checking secondary criteria only when earlier criteria compare as equal.
Details
If any of the Orderings is non-zero, the result is the first non-zero Ordering.
If all the Orderings are zero, the result is zero.
Gotchas
combineAll stops consuming the iterable as soon as it finds a non-zero
Ordering.
export const const Reducer: Reducer_.Reducer<Ordering>const Reducer: {
initialValue: A;
combineAll: (collection: Iterable<A>) => A;
combine: (self: A, that: A) => A;
}
Reducer for combining Orderings.
When to use
Use to combine multiple comparison results in priority order, such as
checking secondary criteria only when earlier criteria compare as equal.
Details
If any of the Orderings is non-zero, the result is the first non-zero Ordering.
If all the Orderings are zero, the result is zero.
Gotchas
combineAll stops consuming the iterable as soon as it finds a non-zero
Ordering.
Reducer: import Reducer_Reducer_.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 Ordering = 0 | 1 | -1Represents the result of comparing two values.
When to use
Use to model a normalized comparison result that is exactly less than,
equal to, or greater than.
Details
-1 indicates the first value is less than the second
0 indicates the values are equal
1 indicates the first value is greater than the second
Example (Defining comparison results)
import type { Ordering } from "effect"
// Custom comparison function
const compareNumbers = (a: number, b: number): Ordering.Ordering => {
if (a < b) return -1
if (a > b) return 1
return 0
}
console.log(compareNumbers(5, 10)) // -1 (5 < 10)
console.log(compareNumbers(10, 5)) // 1 (10 > 5)
console.log(compareNumbers(5, 5)) // 0 (5 == 5)
// Using with string comparison
const compareStrings = (a: string, b: string): Ordering.Ordering => {
return a.localeCompare(b) as Ordering.Ordering
}
Ordering> = import Reducer_Reducer_.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<type Ordering = 0 | 1 | -1Represents the result of comparing two values.
When to use
Use to model a normalized comparison result that is exactly less than,
equal to, or greater than.
Details
-1 indicates the first value is less than the second
0 indicates the values are equal
1 indicates the first value is greater than the second
Example (Defining comparison results)
import type { Ordering } from "effect"
// Custom comparison function
const compareNumbers = (a: number, b: number): Ordering.Ordering => {
if (a < b) return -1
if (a > b) return 1
return 0
}
console.log(compareNumbers(5, 10)) // -1 (5 < 10)
console.log(compareNumbers(10, 5)) // 1 (10 > 5)
console.log(compareNumbers(5, 5)) // 0 (5 == 5)
// Using with string comparison
const compareStrings = (a: string, b: string): Ordering.Ordering => {
return a.localeCompare(b) as Ordering.Ordering
}
Ordering>(
(self: Orderingself, that: Orderingthat) => self: Orderingself !== 0 ? self: 1 | -1self : that: Orderingthat,
0,
(collection: Iterable<Ordering>collection) => {
let let ordering: Orderingordering: type Ordering = 0 | 1 | -1Represents the result of comparing two values.
When to use
Use to model a normalized comparison result that is exactly less than,
equal to, or greater than.
Details
-1 indicates the first value is less than the second
0 indicates the values are equal
1 indicates the first value is greater than the second
Example (Defining comparison results)
import type { Ordering } from "effect"
// Custom comparison function
const compareNumbers = (a: number, b: number): Ordering.Ordering => {
if (a < b) return -1
if (a > b) return 1
return 0
}
console.log(compareNumbers(5, 10)) // -1 (5 < 10)
console.log(compareNumbers(10, 5)) // 1 (10 > 5)
console.log(compareNumbers(5, 5)) // 0 (5 == 5)
// Using with string comparison
const compareStrings = (a: string, b: string): Ordering.Ordering => {
return a.localeCompare(b) as Ordering.Ordering
}
Ordering = 0
for (let ordering: Orderingordering of collection: Iterable<Ordering>collection) {
if (let ordering: Orderingordering !== 0) {
return let ordering: Orderingordering
}
}
return let ordering: Orderingordering
}
)