<const Elements extends ReadonlyArray<Order<any>>>(
elements: Elements
): Order<{
readonly [I in keyof Elements]: [Elements[I]] extends [Order<infer A>]
? A
: never
}>Creates an Order for a tuple type based on orders for each element.
When to use
Use when you need fixed-length tuple ordering with per-position orders.
Details
Compares tuples element-by-element using the corresponding order and stops at
the first non-zero comparison result. Tuples must have the same length as the
order collection, and the result is 0 only if all elements are equal.
Example (Ordering tuples)
import { Order } from "effect"
const tupleOrder = Order.Tuple([Order.Number, Order.String])
console.log(tupleOrder([1, "a"], [2, "b"])) // -1
console.log(tupleOrder([1, "b"], [1, "a"])) // 1
console.log(tupleOrder([1, "a"], [1, "a"])) // 0export function function Tuple<
Elements extends ReadonlyArray<Order<any>>
>(
elements: Elements
): Order<{
readonly [I in keyof Elements]: [
Elements[I]
] extends [Order<infer A>]
? A
: never
}>
Creates an Order for a tuple type based on orders for each element.
When to use
Use when you need fixed-length tuple ordering with per-position orders.
Details
Compares tuples element-by-element using the corresponding order and stops at
the first non-zero comparison result. Tuples must have the same length as the
order collection, and the result is 0 only if all elements are equal.
Example (Ordering tuples)
import { Order } from "effect"
const tupleOrder = Order.Tuple([Order.Number, Order.String])
console.log(tupleOrder([1, "a"], [2, "b"])) // -1
console.log(tupleOrder([1, "b"], [1, "a"])) // 1
console.log(tupleOrder([1, "a"], [1, "a"])) // 0
Tuple<const function (type parameter) Elements in Tuple<const Elements extends ReadonlyArray<Order<any>>>(elements: Elements): Order<{ readonly [I in keyof Elements]: [Elements[I]] extends [Order<infer A>] ? A : never; }>Elements extends interface ReadonlyArray<T>ReadonlyArray<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<any>>>(
elements: const Elements extends ReadonlyArray<Order<any>>elements: function (type parameter) Elements in Tuple<const Elements extends ReadonlyArray<Order<any>>>(elements: Elements): Order<{ readonly [I in keyof Elements]: [Elements[I]] extends [Order<infer A>] ? A : never; }>Elements
): 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<{ readonly [function (type parameter) II in keyof function (type parameter) Elements in Tuple<const Elements extends ReadonlyArray<Order<any>>>(elements: Elements): Order<{ readonly [I in keyof Elements]: [Elements[I]] extends [Order<infer A>] ? A : never; }>Elements]: [function (type parameter) Elements in Tuple<const Elements extends ReadonlyArray<Order<any>>>(elements: Elements): Order<{ readonly [I in keyof Elements]: [Elements[I]] extends [Order<infer A>] ? A : never; }>Elements[function (type parameter) II]] extends [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<infer function (type parameter) AA>] ? function (type parameter) AA : never }> {
return function make<A>(
compare: (self: A, that: A) => -1 | 0 | 1
): Order<A>
Creates a new Order instance from a comparison function.
When to use
Use when you need a sorting rule not covered by the built-in orders or input
mapping helpers, and you can provide a total comparison.
Details
Uses reference equality (===) as a shortcut: if self === that, it returns
0 without calling the comparison function. The comparison function should
return -1, 0, or 1, and the returned order satisfies total ordering
laws when the comparison function does.
Example (Creating an Order)
import { Order } from "effect"
const byAge = Order.make<{ name: string; age: number }>((self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
})
console.log(byAge({ name: "Alice", age: 30 }, { name: "Bob", age: 25 })) // 1
console.log(byAge({ name: "Alice", age: 25 }, { name: "Bob", age: 30 })) // -1
make((self: {
readonly [I in keyof Elements]: [
Elements[I]
] extends [Order<infer A>]
? A
: never
}
self, that: {
readonly [I in keyof Elements]: [
Elements[I]
] extends [Order<infer A>]
? A
: never
}
that) => {
const const len: numberlen = elements: const Elements extends ReadonlyArray<Order<any>>elements.ReadonlyArray<Order<any>>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length
for (let let i: numberi = 0; let i: numberi < const len: numberlen; let i: numberi++) {
const const o: Orderingo = elements: const Elements extends ReadonlyArray<Order<any>>elements[let i: numberi](self: {
readonly [I in keyof Elements]: [
Elements[I]
] extends [Order<infer A>]
? A
: never
}
self[let i: numberi], that: {
readonly [I in keyof Elements]: [
Elements[I]
] extends [Order<infer A>]
? A
: never
}
that[let i: numberi])
if (const o: Orderingo !== 0) {
return const o: 1 | -1o
}
}
return 0
})
}