Base.ComparableDefines functors for making modules comparable.
Usage example:
module Foo = struct
module T = struct
type t = ... [@@deriving compare, sexp]
end
include T
include Comparable.Make (T)
endThen include Comparable.S in the signature
module Foo : sig
type t = ...
include Comparable.S with type t := t
endTo add an Infix submodule:
module C = Comparable.Make (T)
include C
module Infix = (C : Comparable.Infix with type t := t)A common pattern is to define a module O with a restricted signature. It aims to be (locally) opened to bring useful operators into scope without shadowing unexpected variable names. E.g., in the Date module:
module O = struct
include (C : Comparable.Infix with type t := t)
let to_string t = ..
endOpening Date would shadow now, but opening Date.O doesn't:
let now = .. in
let someday = .. in
Date.O.(now > someday)module type Infix = Comparisons.Infixmodule type S = sig ... endmodule type Polymorphic_compare = Comparisons.Smodule type Validate = sig ... endmodule type With_zero = sig ... endlexicographic cmps x y compares x and y lexicographically using functions in the list cmps.
lift cmp ~f x y compares x and y by comparing f x and f y via cmp.
reverse cmp x y = cmp y x
Reverses the direction of asymmetric relations by swapping their arguments. Useful, e.g., for relations implementing "is a subset of" or "is a descendant of".
Where reversed relations are already provided, use them directly. For example, Comparable.S provides ascending and descending, which are more readable as a pair than compare and reverse compare. Similarly, <= is more idiomatic than reverse (>=).
The functions below are analogues of the type-specific functions exported by the Comparable.S interface.
Derive Infix or Polymorphic_compare functions from just [@@deriving compare], without need for the sexp_of_t required by Make* (see below).
module Polymorphic_compare (T : sig ... end) : Polymorphic_compare with type t := T.tInherit comparability from a component.
module Make_using_comparator (T : sig ... end) : S with type t := T.t with type comparator_witness := T.comparator_witnessmodule Validate_with_zero (T : sig ... end) : sig ... end