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
'a
s and we know how to convert'b
to'a
, we can compare'b
s by first converting our'b
s to'a
s, 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 forby
and the foundational function of theContravariant
type class instance for thecompare
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, returning1
if the first value is greater than the second,-1
if the first is less than the second, and0
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 forlessThanBy
.
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 forlessThan
.
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 forlessThanOrEqBy
.
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 forlessThanOrEq
.
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 forgreaterThanBy
.
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 forgreaterThan
.
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 forgreaterThanOrEqBy
.
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 forgreaterThanOrEq
.
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 ofclampBy
.
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 ofbetweenBy
.
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 haszero
andsubtract
). If the provided value is greater than (or equal to) zero, that value is returned, otherwise the negated version of that value is returned.