diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 15 | ||||
-rw-r--r-- | src/gleam/int.gleam | 15 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index f3944f3..a672eca 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -29,6 +29,21 @@ pub fn to_string(f: Float) -> String { |> string_builder.to_string } +/// Restrict a Float between a lower and upper bound +/// +/// ## Examples +/// +/// ``` +/// > clamp(1.2, min: 1.4, max: 1.6) +/// 1.4 +/// ``` +/// +pub fn clamp(n: Float, min min_bound: Float, max max_bound: Float) -> Float { + n + |> min(max_bound) + |> max(min_bound) +} + /// Compares two floats, returning an order. /// /// ## Examples diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index a4a956a..96b4405 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -75,6 +75,21 @@ pub external fn to_base_string(Int, Int) -> String = pub external fn to_float(a: Int) -> Float = "erlang" "float" +/// Restrict an Int between a lower and upper bound +/// +/// ## Examples +/// +/// ``` +/// > clamp(40, min: 50, max: 60) +/// 50 +/// ``` +/// +pub fn clamp(n: Int, min min_bound: Int, max max_bound: Int) -> Int { + n + |> min(max_bound) + |> max(min_bound) +} + /// Compares two ints, returning an order. /// /// ## Examples |