<B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>
<A, B>(self: Order<A>, f: (b: B) => A): Order<B>Transforms an Order on type A into an Order on type B by providing a function that
maps values of type B to values of type A.
When to use
Use when you need to adapt an Order to compare a larger value by one
derived property.
Details
Applies the mapping function to both values before comparison. The mapping function should be pure and not have side effects so the ordering properties of the original order are preserved.
Example (Mapping Input)
import { Order } from "effect"
const byLength = Order.mapInput(Order.Number, (s: string) => s.length)
console.log(byLength("a", "bb")) // -1
console.log(byLength("bb", "a")) // 1
console.log(byLength("aa", "bb")) // 0export const const mapInput: {
<B, A>(f: (b: B) => A): (
self: Order<A>
) => Order<B>
<A, B>(self: Order<A>, f: (b: B) => A): Order<B>
}
Transforms an Order on type A into an Order on type B by providing a function that
maps values of type B to values of type A.
When to use
Use when you need to adapt an Order to compare a larger value by one
derived property.
Details
Applies the mapping function to both values before comparison. The mapping
function should be pure and not have side effects so the ordering properties
of the original order are preserved.
Example (Mapping Input)
import { Order } from "effect"
const byLength = Order.mapInput(Order.Number, (s: string) => s.length)
console.log(byLength("a", "bb")) // -1
console.log(byLength("bb", "a")) // 1
console.log(byLength("aa", "bb")) // 0
mapInput: {
<function (type parameter) B in <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>B, function (type parameter) A in <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>A>(f: (b: B) => Af: (b: Bb: function (type parameter) B in <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>B) => function (type parameter) A in <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>A): (self: Order<A>self: 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 <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>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<function (type parameter) B in <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>B>
<function (type parameter) A in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>A, function (type parameter) B in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>B>(self: Order<A>self: 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 <A, B>(self: Order<A>, f: (b: B) => A): Order<B>A>, f: (b: B) => Af: (b: Bb: function (type parameter) B in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>B) => function (type parameter) A in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>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<function (type parameter) B in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>B>
} = dual<(...args: Array<any>) => any, <A, B>(self: Order<A>, f: (b: B) => A) => Order<B>>(arity: 2, body: <A, B>(self: Order<A>, f: (b: B) => A) => Order<B>): ((...args: Array<any>) => any) & (<A, B>(self: Order<A>, f: (b: B) => A) => Order<B>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(
2,
<function (type parameter) A in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>A, function (type parameter) B in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>B>(self: Order<A>self: 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 <A, B>(self: Order<A>, f: (b: B) => A): Order<B>A>, f: (b: B) => Af: (b: Bb: function (type parameter) B in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>B) => function (type parameter) A in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>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<function (type parameter) B in <A, B>(self: Order<A>, f: (b: B) => A): Order<B>B> => 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((b1: Bb1, b2: Bb2) => self: Order<A>self(f: (b: B) => Af(b1: Bb1), f: (b: B) => Af(b2: Bb2)))
)