<A>(): Reducer.Reducer<Order<A>>Creates a Reducer for combining Order instances, useful for aggregating orders in collections.
When to use
Use when you need a reducer that combines orders.
Details
Returns a reducer that combines orders using combine, uses alwaysEqual as
the identity element for empty collections, and uses combineAll for
combining collections of orders. The reducer can be used with fold operations
on collections.
Example (Creating a Reducer)
import { Order } from "effect"
const reducer = Order.makeReducer<number>()
const orders = [Order.Number, Order.flip(Order.Number)]
const combined = reducer.combineAll(orders)
console.log(combined(1, 2)) // -1 (uses first order)export function function makeReducer<
A
>(): Reducer.Reducer<Order<A>>
Creates a Reducer for combining Order instances, useful for aggregating orders in collections.
When to use
Use when you need a reducer that combines orders.
Details
Returns a reducer that combines orders using combine, uses alwaysEqual as
the identity element for empty collections, and uses combineAll for
combining collections of orders. The reducer can be used with fold operations
on collections.
Example (Creating a Reducer)
import { Order } from "effect"
const reducer = Order.makeReducer<number>()
const orders = [Order.Number, Order.flip(Order.Number)]
const combined = reducer.combineAll(orders)
console.log(combined(1, 2)) // -1 (uses first order)
makeReducer<function (type parameter) A in makeReducer<A>(): Reducer.Reducer<Order<A>>A>() {
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<interface Order<in A>Represents a total ordering for values of type A.
When to use
Use when you need to define how values of a type are compared.
Details
An order returns -1 when the first value is less than the second, 0 when
the values are equal according to this ordering, and 1 when the first value
is greater than the second. It must satisfy total ordering laws: totality,
antisymmetry, and transitivity.
Example (Defining a custom Order)
import { Order } from "effect"
const byAge: Order.Order<{ name: string; age: number }> = (self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
}
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
console.log(byAge(person1, person2)) // 1
Order<function (type parameter) A in makeReducer<A>(): Reducer.Reducer<Order<A>>A>>(
const combine: {
<A>(that: Order<A>): (
self: Order<A>
) => Order<A>
<A>(self: Order<A>, that: Order<A>): Order<A>
}
Combines two Order instances to create a new Order that first compares using the first Order,
and if the values are equal, then compares using the second Order.
When to use
Use when you need tie-breaking with exactly two orders.
Details
First applies the first order. If the result is non-zero, that result is
returned; otherwise, the second order is applied. The result is the first
non-zero comparison result, or 0 if both orders return 0.
Example (Combining two Orders)
import { Order } from "effect"
const byAge = Order.mapInput(
Order.Number,
(person: { name: string; age: number }) => person.age
)
const byName = Order.mapInput(
Order.String,
(person: { name: string; age: number }) => person.name
)
const byAgeAndName = Order.combine(byAge, byName)
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 30 }
const person3 = { name: "Charlie", age: 25 }
console.log(byAgeAndName(person1, person2)) // -1 (Same age, Alice < Bob)
console.log(byAgeAndName(person1, person3)) // 1 (Alice (30) > Charlie (25))
combine,
() => 0,
function combineAll<A>(
collection: Iterable<Order<A>>
): Order<A>
Combines all Order instances in the provided collection into a single Order.
The resulting Order compares using each Order in sequence until a non-zero result is found.
When to use
Use when you need tie-breaking across a variable number of orders.
Details
Applies orders in iteration order and short-circuits on the first non-zero
result. It returns 0 only if all orders return 0.
Example (Combining multiple Orders)
import { Order } from "effect"
const byAge = Order.mapInput(
Order.Number,
(person: { name: string; age: number }) => person.age
)
const byName = Order.mapInput(
Order.String,
(person: { name: string; age: number }) => person.name
)
const combinedOrder = Order.combineAll([byAge, byName])
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 30 }
console.log(combinedOrder(person1, person2)) // -1 (Same age, Alice < Bob)
combineAll
)
}