Or

Orders in Gleam

4 exercises

About Orders

The gleam/order module in Gleam's standard library contains the Order type, which is used to compare values.

The definition of Order looks like this:

pub type Order {
  Gt
  Eq
  Lt
}

The type has three variants, each used to represent the relationship between value and another value:

  • Gt - the value is larger than the other value.
  • Eq - the value is equal to the other value.
  • Lt - the value is smaller than the other value.

If a data type can be compared by size it is common for the module to define a compare function which returns an Order value. For example, gleam/float and gleam/int define compare functions that operate on floats and ints respectively.

int.compare(1, 2)
// -> Lt

int.compare(2, 2)
// -> Eq

float.compare(1.0, 2.0)
// -> Lt

Sorting functions such as list.sort commonly take a function that returns an Order value, allowing the caller to define how the list should be sorted.

list.sort([3, 7, 3, 1], by: int.compare)
// -> [1, 3, 3, 7]
Edit via GitHub The link opens in a new window or tab

Learn Orders

Practicing is locked

Unlock 2 more exercises to practice Orders