Hyperlinkv0.8.0-beta.28

Order

Order.combineconsteffect/Order.ts:315
<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))
Source effect/Order.ts:31511 lines
export const combine: {
  <A>(that: Order<A>): (self: Order<A>) => Order<A>
  <A>(self: Order<A>, that: Order<A>): Order<A>
} = dual(2, <A>(self: Order<A>, that: Order<A>): Order<A> =>
  make((a1, a2) => {
    const out = self(a1, a2)
    if (out !== 0) {
      return out
    }
    return that(a1, a2)
  }))
Referenced by 1 symbols