Module type Ordered0.Extension

Extended interface for ordered types.

type t

The type for ordered values.

val compare : t comparator

Returns an Ordering between two values.

include Equal0.Extension with type t := t
include Equal0
type t

Abstract type for equatable values.

val equal : t equality

equal t1 t2 tests if the values t1 and t2 are equal, and is used by the = and <> operators.

val not_equal : t equality

not_equal t1 t2 tests if the values t1 and t2 are not equal, and is used by <>.

not_eq t1 t2 is a shorthand for not (equal t1 t2).

val (=) : t equality

Operator alias for equal.

val (<>) : t equality

Operator alias for not_equal.

val (<) : t -> t -> bool

Test whether one value is strictly less than another.

val (>) : t -> t -> bool

Test whether one value is strictly greater than another.

val (<=) : t -> t -> bool

Test whether one value is non-strictly less than another.

val (>=) : t -> t -> bool

Test whether one value is non-strictly greater than another.

val min : t -> t -> t

Take the minimum of two values. If they are considered equal, the first argument is chosen.

val max : t -> t -> t

Take the maximum of two values. If they are considered equal, the first argument is chosen.

val comparing : ('a -> t) -> 'a comparator

Applies a projection function to obtain a comparable value of type t.

comparing f is defined as Comparator.by f compare.

Examples

type person = { name : string; age : int }

let by_age_descending : person comparator =
  Comparator.descending (Ordered_int.comparing (fun p -> p.age))

let () =
  [{ name = "Alice"; age = 24 };
   { name = "Bob";   age = 19 };
   { name = "Craig"; age = 45 }]
  |> List.sort (Comparator.to_integral by_age_descending)
  |> List.iter (fun p -> print_endline p.name)
val between : min:t -> max:t -> t -> bool

between ~min ~max x tests whether the value x is between a minimum and a maximum (inclusive on both ends).

Examples

let f = between ~min:0 ~max:10 in
assert (f 0);
assert (f 3);
assert (not (f 15);
assert (not (f (-5)))
val clamp : min:t -> max:t -> t -> t

clamp ~min ~max x clamps the value x between a minimum and a maximum.

Examples

let f = clamp ~min:0 ~max:10
assert (f (-5) = 0);
assert (f 5 = 5);
assert (f 15 = 10)