<A>(O: Order<A>): Order<ReadonlyArray<A>>function function Array_<A>(
O: Order<A>
): Order<ReadonlyArray<A>>
Array_<function (type parameter) A in Array_<A>(O: Order<A>): Order<ReadonlyArray<A>>A>(type O: Order<A>O: 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 Array_<A>(O: Order<A>): Order<ReadonlyArray<A>>A>): 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<interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in Array_<A>(O: Order<A>): Order<ReadonlyArray<A>>A>> {
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 A[]self, that: readonly A[]that) => {
const const aLen: numberaLen = self: readonly A[]self.ReadonlyArray<T>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length
const const bLen: numberbLen = that: readonly A[]that.ReadonlyArray<T>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length
const const len: numberlen = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.min(...values: number[]): numberReturns the smaller of a set of supplied numeric expressions.
min(const aLen: numberaLen, const bLen: numberbLen)
for (let let i: numberi = 0; let i: numberi < const len: numberlen; let i: numberi++) {
const const o: Orderingo = type O: Order<A>O(self: readonly A[]self[let i: numberi], that: readonly A[]that[let i: numberi])
if (const o: Orderingo !== 0) {
return const o: 1 | -1o
}
}
return const Number: Order<number>Order instance for numbers that compares them numerically.
When to use
Use when you need numeric ordering for numbers.
Details
0 is considered equal to -0. All NaN values are considered equal to
each other, and any NaN is considered less than any non-NaN number. All
other values use standard numeric comparison.
Example (Ordering numbers)
import { Order } from "effect"
console.log(Order.Number(1, 1)) // 0
console.log(Order.Number(1, 2)) // -1
console.log(Order.Number(2, 1)) // 1
console.log(Order.Number(0, -0)) // 0
console.log(Order.Number(NaN, 1)) // -1
Number(const aLen: numberaLen, const bLen: numberbLen)
})
}