aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian <s@porto5.com>2021-01-19 14:30:01 +1100
committerLouis Pilfold <louis@lpil.uk>2021-01-19 16:15:24 +0000
commit400cd752db3e04dcad8bf2a199a4d136ed85cedc (patch)
tree1b16c022923c2490204d426ee9040ed66d0f881b /src
parentf78f931d642a3515452c8164c71e86670a6d7401 (diff)
downloadgleam_stdlib-400cd752db3e04dcad8bf2a199a4d136ed85cedc.tar.gz
gleam_stdlib-400cd752db3e04dcad8bf2a199a4d136ed85cedc.zip
Add int.clamp and float.clamp
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam15
-rw-r--r--src/gleam/int.gleam15
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