Module Relude_Ord

Relude.Ord contains functions and sub-modules to help work with comparison functions that take two values of the same type and return a value of Bastet's ordering type.

type nonrec ordering = BsBastet.Interface.ordering;

The ordering type represents the result of a comparison - `less_than, `equal_to, or `greater_than.

type compare('a) = 'a => 'a => ordering;

The compare function is the heart of the Ord type class.

let by: a b. ('b => 'a) => compare('a) => compare('b);

Ord.by converts an compare function for a type 'a to a compare function of type 'b using a function from 'b => 'a.

If we know how to compare 'as and we know how to convert 'b to 'a, we can compare 'bs by first converting our 'bs to 'as, then comparing them with the 'a compare function.

This is the contravariant map for the compare function.

let userCompare: compare(user) = Ord.by(user => user.name, String.compare);
let cmap: ('a => 'b) => compare('b) => compare('a);

Ord.cmap is an alias for by and the foundational function of the Contravariant type class instance for the compare function.

module Contravariant: BsBastet.Interface.CONTRAVARIANT with type Contravariant.t('a) = compare('a);
include { ... };
let reverse: a. compare('a) => compare('a);

Ord.reverse creates a new compare function that returns the opposite of the given compare function.

let compareAsIntBy: a. compare('a) => 'a => 'a => int;

Ord.compareAsIntBy compares two values using the given compare function, returning 1 if the first value is greater than the second, -1 if the first is less than the second, and 0 if they are equal.

While not often needed in the Relude/Bastet ecosystem, this function may be useful for translating a Bastet-compatible compare function to one that works with the rest of the OCaml world.

let compareAsInt: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => int;

Compares two values using the given ORD module

let minBy: a. compare('a) => 'a => 'a => 'a;

Finds the minimum of two values using the given compare function

let min: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => 'a;

Finds the minimum of two values using the given ORD module

let maxBy: a. compare('a) => 'a => 'a => 'a;

Finds the maximum of two values using the given compare function

let max: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => 'a;

Finds the maximum of two values using the given ORD module

let lessThanBy: a. compare('a) => 'a => 'a => bool;

Indicates if the item on the left is less than the item on the right using the given compare function.

let ltBy: compare('a) => 'a => 'a => bool;

Ord.ltBy is an alias for lessThanBy.

let lessThan: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.lessThan indicates if the item on the left is less than the item on the right using the given ORD module.

let lt: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.lt is an alias for lessThan.

let lessThanOrEqBy: a. compare('a) => 'a => 'a => bool;

Ord.lessThanOrEqBy indicates if the item on the left is less than or equal to the item on the right using the given compare function.

let lteBy: compare('a) => 'a => 'a => bool;

Ord.lteBy is an alias for lessThanOrEqBy.

let lessThanOrEq: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.lessThanOrEq indicates if the item on the left is less than or equal to the item on the right using the given ORD module.

let lte: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.lte is an alias for lessThanOrEq.

let greaterThanBy: a. compare('a) => 'a => 'a => bool;

Ord.greaterThanBy indicates if the item on the left is greater than the item on the right using the given compare function.

let gtBy: compare('a) => 'a => 'a => bool;

Ord.gtBy is an alias for greaterThanBy.

let greaterThan: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.greaterThan indicates if the item on the left is greater than the item on the right using the given ORD module.

let gt: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.gt is an alias for greaterThan.

let greaterThanOrEqBy: a. compare('a) => 'a => 'a => bool;

Ord.greaterThanOrEqBy indicates if the item on the left is greater than or equal to the item on the right using the given compare function.

let gteBy: compare('a) => 'a => 'a => bool;

Ord.gteBy is an alias for greaterThanOrEqBy.

let greaterThanOrEq: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.greaterThanOrEq indicates if the item on the left is greater than or equal to the item on the right using the given ORD module.

let gte: (module BsBastet.Interface.ORD with type t = 'a) => 'a => 'a => bool;

Ord.gte is an alias for greaterThanOrEq.

let clampBy: a. compare('a) => min:'a => max:'a => 'a => 'a;

Ord.clampBy ensures a provided value falls between a max and min (inclusive).

Note that if the provided min is greater than the provided max, the max is always returned. This is considered an incorrect use of clamp.

let clamp = clampBy(Int.compare);
clamp(~min=0, ~max=5, 3) == 3;
clamp(~min=0, ~max=5, 0) == 0;
clamp(~min=0, ~max=3, 4) == 3;
clamp(~min=1, ~max=0, 2) == 0; // don't do this
let clamp: (module BsBastet.Interface.ORD with type t = 'a) => min:'a => max:'a => 'a => 'a;

Ord.clamp is the first-class module version of clampBy.

let betweenBy: a. compare('a) => min:'a => max:'a => 'a => bool;

Ord.betweenBy determines whether a provided value falls between a min and max.

let between: (module BsBastet.Interface.ORD with type t = 'a) => min:'a => max:'a => 'a => bool;

Ord.between is the first-class module version of betweenBy.

let abs: (module BsBastet.Interface.ORD with type t = 'a) => (module BsBastet.Interface.RING with type t = 'a) => 'a => 'a;

Ord.abs determines the absolute value for any value that can be compared for ordering and implements Ring (specifically has zero and subtract). If the provided value is greater than (or equal to) zero, that value is returned, otherwise the negated version of that value is returned.

let signum: (module BsBastet.Interface.ORD with type t = 'a) => (module BsBastet.Interface.RING with type t = 'a) => 'a => 'a;

Ord.signum is the sign function, which evaluates to one for values >= zero, and negative one for values less than zero.