<A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>
readonly onEqual: LazyArg<B>
readonly onGreaterThan: LazyArg<C>
}): (self: Ordering) => A | B | C
<A, B, C = B>(
o: Ordering,
options: {
readonly onLessThan: LazyArg<A>
readonly onEqual: LazyArg<B>
readonly onGreaterThan: LazyArg<C>
}
): A | B | CMatches an Ordering value and returns the branch selected by that ordering.
When to use
Use to branch on the three possible comparison outcomes in one expression.
Example (Pattern matching on orderings)
import { Function, Ordering } from "effect"
import * as assert from "node:assert"
const toMessage = Ordering.match({
onLessThan: Function.constant("less than"),
onEqual: Function.constant("equal"),
onGreaterThan: Function.constant("greater than")
})
assert.deepStrictEqual(toMessage(-1), "less than")
assert.deepStrictEqual(toMessage(0), "equal")
assert.deepStrictEqual(toMessage(1), "greater than")export const const match: {
<A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>
readonly onEqual: LazyArg<B>
readonly onGreaterThan: LazyArg<C>
}): (self: Ordering) => A | B | C
<A, B, C = B>(
o: Ordering,
options: {
readonly onLessThan: LazyArg<A>
readonly onEqual: LazyArg<B>
readonly onGreaterThan: LazyArg<C>
}
): A | B | C
}
Matches an Ordering value and returns the branch selected by that ordering.
When to use
Use to branch on the three possible comparison outcomes in one expression.
Example (Pattern matching on orderings)
import { Function, Ordering } from "effect"
import * as assert from "node:assert"
const toMessage = Ordering.match({
onLessThan: Function.constant("less than"),
onEqual: Function.constant("equal"),
onGreaterThan: Function.constant("greater than")
})
assert.deepStrictEqual(toMessage(-1), "less than")
assert.deepStrictEqual(toMessage(0), "equal")
assert.deepStrictEqual(toMessage(1), "greater than")
match: {
<function (type parameter) A in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
A, function (type parameter) B in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
B, function (type parameter) C in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
C = function (type parameter) B in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
B>(
options: {
readonly onLessThan: LazyArg<A>
readonly onEqual: LazyArg<B>
readonly onGreaterThan: LazyArg<C>
}
options: {
readonly onLessThan: LazyArg<A>onLessThan: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) A in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
A>
readonly onEqual: LazyArg<B>onEqual: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) B in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
B>
readonly onGreaterThan: LazyArg<C>onGreaterThan: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) C in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
C>
}
): (self: Orderingself: 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) => function (type parameter) A in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
A | function (type parameter) B in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
B | function (type parameter) C in <A, B, C = B>(options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): (self: Ordering) => A | B | C
C
<function (type parameter) A in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
A, function (type parameter) B in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B, function (type parameter) C in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
C = function (type parameter) B in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B>(
o: Orderingo: 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,
options: {
readonly onLessThan: LazyArg<A>
readonly onEqual: LazyArg<B>
readonly onGreaterThan: LazyArg<C>
}
options: {
readonly onLessThan: LazyArg<A>onLessThan: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) A in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
A>
readonly onEqual: LazyArg<B>onEqual: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) B in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B>
readonly onGreaterThan: LazyArg<C>onGreaterThan: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) C in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
C>
}
): function (type parameter) A in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
A | function (type parameter) B in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B | function (type parameter) C in <A, B, C = B>(o: Ordering, options: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
C
} = dual<(...args: Array<any>) => any, <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}) => A | B | C>(arity: 2, body: <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}) => A | B | C): ((...args: Array<any>) => any) & (<A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}) => A | B | C) (+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, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
A, function (type parameter) B in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B, function (type parameter) C in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
C = function (type parameter) B in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B>(
self: Orderingself: 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,
{ onEqual: LazyArg<B>onEqual, onGreaterThan: LazyArg<C>onGreaterThan, onLessThan: LazyArg<A>onLessThan }: {
readonly onLessThan: LazyArg<A>onLessThan: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) A in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
A>
readonly onEqual: LazyArg<B>onEqual: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) B in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B>
readonly onGreaterThan: LazyArg<C>onGreaterThan: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) C in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
C>
}
): function (type parameter) A in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
A | function (type parameter) B in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
B | function (type parameter) C in <A, B, C = B>(self: Ordering, { onEqual, onGreaterThan, onLessThan }: {
readonly onLessThan: LazyArg<A>;
readonly onEqual: LazyArg<B>;
readonly onGreaterThan: LazyArg<C>;
}): A | B | C
C => self: Orderingself === -1 ? onLessThan: LazyArg<A>onLessThan() : self: Orderingself === 0 ? onEqual: LazyArg<B>onEqual() : onGreaterThan: LazyArg<C>onGreaterThan())