cmp

Types

A comparator for values of type a. Returns an order.Order from the stdlib order module.

pub type Comparator(a) =
  fn(a, a) -> order.Order

Values

pub fn by(
  key: fn(a) -> b,
  cmp: fn(b, b) -> order.Order,
) -> fn(a, a) -> order.Order

Generic contramap helper: build a comparator for a by providing a key extractor key and a comparator cmp for the key type b.

Example:

let cmp = cmp.by(fn(u) { #(name, _) -> name }, string.compare)
list.sort(users, by: cmp)
pub fn by_float(key: fn(a) -> Float) -> fn(a, a) -> order.Order

Build a comparator by extracting a Float key from values of type a.

pub fn by_int(key: fn(a) -> Int) -> fn(a, a) -> order.Order

Build a comparator by extracting an Int key from values of type a. Returns order.Order via int.compare.

pub fn by_normalized_string(
  key: fn(a) -> String,
  normalize: fn(String) -> String,
  cmp: fn(String, String) -> order.Order,
) -> fn(a, a) -> order.Order

Normalize keys with normalize before applying cmp. Useful if you want to apply folding/normalization (e.g. ASCII folding) prior to comparing. normalize can be any function (for example from str/extra).

pub fn by_string(key: fn(a) -> String) -> fn(a, a) -> order.Order

Build a comparator by extracting a String key from values of type a. Uses the stdlib string.compare which returns order.Order.

pub fn by_string_with(
  key: fn(a) -> String,
  cmp: fn(String, String) -> order.Order,
) -> fn(a, a) -> order.Order

Sugar for string comparators: pass a string key extractor and a string comparator (e.g. string.compare or str.compare). This helper does not import or depend on str; it only accepts a comparator function as an argument so the user may pass str.compare if they use the str package.

pub fn chain(
  comps: List(fn(a, a) -> order.Order),
) -> fn(a, a) -> order.Order

Chain a list of comparators lexicographically. The returned comparator applies each comparator in order and uses the first non-Eq result, or Eq if all comparators are Eq.

pub fn lazy_then(
  comp1: fn(a, a) -> order.Order,
  comp2_thunk: fn() -> fn(a, a) -> order.Order,
) -> fn(a, a) -> order.Order

Lazy tie-breaker: evaluate comp2_thunk only if comp1(x, y) yields Eq. This is useful when building the fallback comparator is expensive.

Example:

let cmp = cmp.lazy_then(by_name, fn() { by_age })
pub fn list_compare(
  elem_cmp: fn(a, a) -> order.Order,
) -> fn(List(a), List(a)) -> order.Order

Compare two lists lexicographically given an element comparator. Empty list is considered less than a non-empty list.

pub fn natural_float(a: Float, b: Float) -> order.Order

Compare two floats using the stdlib float.compare.

pub fn natural_int(a: Int, b: Int) -> order.Order

Compare two integers using the stdlib int.compare.

pub fn natural_string(a: String, b: String) -> order.Order

Compare two strings in the natural (lexicographic) way.

pub fn option(
  none_order: order.Order,
  comp: fn(a, a) -> order.Order,
) -> fn(option.Option(a), option.Option(a)) -> order.Order

Build a comparator for Option(a) given:

  • none_order: the order.Order to use when comparing None with Some(_). Use order.Lt for None < Some, order.Gt for None > Some.
  • comp: comparator for the inner a values (used when both are Some).

Example:

let c = cmp.option(order.Lt, cmp.by_int(.age))
pub fn pair(
  a_cmp: fn(a, a) -> order.Order,
  b_cmp: fn(b, b) -> order.Order,
) -> fn(#(a, b), #(a, b)) -> order.Order

Compare two pairs (tuples of two elements) using provided comparators for the first and second element respectively. Useful for common (a, b) sorting patterns.

pub fn reverse(
  comp: fn(a, a) -> order.Order,
) -> fn(a, a) -> order.Order

Reverse the ordering produced by comp. Delegates to order.reverse for correctness.

pub fn then(
  comp1: fn(a, a) -> order.Order,
  comp2: fn(a, a) -> order.Order,
) -> fn(a, a) -> order.Order

Compose two comparators: try comp1, if Eq fall back to comp2. The returned comparator is type-safe and total.

pub fn triple(
  a_cmp: fn(a, a) -> order.Order,
  b_cmp: fn(b, b) -> order.Order,
  c_cmp: fn(c, c) -> order.Order,
) -> fn(#(a, b, c), #(a, b, c)) -> order.Order

Compare three-element tuples lexicographically using three comparators.

Search Document