Constants in Gleam are a way to give names to values. They are defined using the const
keyword.
pub const pi: Float = 3.14159
pub fn circle_area(radius: Float) -> Float {
radius *. radius *. pi
}
When defined with the pub
keyword module constants can be accessed from other modules, otherwise they can only be accessed from within the module they are defined in.
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]
Tori is a climate journalist and she wants to write an article about the weather in different cities. She has data about the weather in different cities and she wants to rank them based on their temperature, but some of the temperatures are in Fahrenheit and some are in Celsius. She needs your help to convert the temperatures to the same unit and rank them.
Define a the fahrenheit_to_celsius
function that takes a temperature in Fahrenheit as an argument and returns the temperature in Celsius.
To convert Fahrenheit to Celsius subtract 32 from the Fahrenheit value, and then divide the result by 1.8.
fahrenheit_to_celsius(72.5)
// -> 22.5
Define the compare_temperature
function that takes two temperatures and returns an Order
value indicating whether the first temperature is less than, equal to, or greater than the second temperature.
compare_temperature(Celsius(30.5), Fahrenheit(82.1))
// -> Gt
Define the sort_cities_by_temperature
function that takes a list of cities and returns them from coldest to warmest.
sort_cities_by_temperature([
#("London", Celsius(30.5)),
#("Paris", Fahrenheit(82.1))
])
// -> [
// #("Paris", Fahrenheit(82.1)),
// #("London", Celsius(30.5))
// ]
Sign up to Exercism to learn and master Gleam with 35 concepts, 121 exercises, and real human mentoring, all for free.