From f81a92aa87ab230c5aaa144f9530f2adf1d32102 Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 18 Sep 2025 00:04:53 +0700 Subject: [PATCH 1/3] fix: Fixed #867 `int.clamp` and `float.clamp` in some case could be seen as misleading. --- src/gleam/float.gleam | 22 ++++++++++++++++------ src/gleam/int.gleam | 18 ++++++++++++------ test/gleam/float_test.gleam | 8 +++++--- test/gleam/int_test.gleam | 11 ++++++----- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 83bfa6e6e..cec5f25c9 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -63,19 +63,29 @@ pub fn parse(string: String) -> Result(Float, Nil) @external(javascript, "../gleam_stdlib.mjs", "float_to_string") pub fn to_string(x: Float) -> String -/// Restricts a `Float` between a lower and upper bound. +/// Restricts a `Float` between two bounds. /// /// ## Examples /// /// ```gleam -/// clamp(1.2, min: 1.4, max: 1.6) +/// clamp(1.2, start: 1.4, stop: 1.6) /// // -> 1.4 /// ``` /// -pub fn clamp(x: Float, min min_bound: Float, max max_bound: Float) -> Float { - x - |> min(max_bound) - |> max(min_bound) +/// ```gleam +/// clamp(1.2, start: 1.4, stop: 0.6) +/// // -> 1.2 +/// ``` +/// +pub fn clamp( + x: Float, + start start_bound: Float, + stop stop_bound: Float, +) -> Float { + case start_bound >=. stop_bound { + True -> x |> min(start_bound) |> max(stop_bound) + False -> x |> min(stop_bound) |> max(start_bound) + } } /// Compares two `Float`s, returning an `Order`: diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 060ea8905..a980c8856 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -277,19 +277,25 @@ pub fn to_base36(x: Int) -> String { @external(javascript, "../gleam_stdlib.mjs", "identity") pub fn to_float(x: Int) -> Float -/// Restricts an int between a lower and upper bound. +/// Restricts an int between two bounds. /// /// ## Examples /// /// ```gleam -/// clamp(40, min: 50, max: 60) +/// clamp(40, start: 50, stop: 60) /// // -> 50 /// ``` /// -pub fn clamp(x: Int, min min_bound: Int, max max_bound: Int) -> Int { - x - |> min(max_bound) - |> max(min_bound) +/// ```gleam +/// clamp(40, start: 50, stop: 30) +/// // -> 40 +/// ``` +/// +pub fn clamp(x: Int, start start_bound: Int, stop stop_bound: Int) -> Int { + case start_bound >= stop_bound { + True -> x |> min(start_bound) |> max(stop_bound) + False -> x |> min(stop_bound) |> max(start_bound) + } } /// Compares two ints, returning an order. diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index d4eb0a93e..8ef319111 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -61,11 +61,13 @@ pub fn to_string_test() { } pub fn clamp_test() { - assert float.clamp(1.4, min: 1.3, max: 1.5) == 1.4 + assert float.clamp(1.4, start: 1.3, stop: 1.5) == 1.4 - assert float.clamp(1.2, min: 1.3, max: 1.5) == 1.3 + assert float.clamp(1.2, start: 1.3, stop: 1.5) == 1.3 - assert float.clamp(1.6, min: 1.3, max: 1.5) == 1.5 + assert float.clamp(1.6, start: 1.3, stop: 1.5) == 1.5 + + assert float.clamp(1.2, start: 1.4, stop: 0.6) == 1.2 } pub fn compare_test() { diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index 26ae9ec07..f09e98450 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -9,14 +9,15 @@ pub fn absolute_value_test() { } pub fn clamp_test() { - assert int.clamp(40, min: 30, max: 50) == 40 + assert int.clamp(40, start: 30, stop: 50) == 40 - assert int.clamp(20, min: 30, max: 50) == 30 + assert int.clamp(20, start: 30, stop: 50) == 30 - assert int.clamp(60, min: 30, max: 50) == 50 + assert int.clamp(60, start: 30, stop: 50) == 50 - // If the bounds are reversed we return the min - assert int.clamp(100, min: 50, max: 30) == 50 + assert int.clamp(100, start: 50, stop: 30) == 50 + + assert int.clamp(40, start: 50, stop: 30) == 40 } pub fn to_string_test() { From 3a8252159268251c93fad87275e7c6f2dec5f447 Mon Sep 17 00:00:00 2001 From: NNB Date: Thu, 18 Sep 2025 00:19:57 +0700 Subject: [PATCH 2/3] fix: Fixed `int.clamp` and `float.clamp` arguments naming. --- src/gleam/float.gleam | 10 +++------- src/gleam/int.gleam | 6 +++--- test/gleam/float_test.gleam | 8 ++++---- test/gleam/int_test.gleam | 10 +++++----- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index cec5f25c9..d649b8c31 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -68,20 +68,16 @@ pub fn to_string(x: Float) -> String /// ## Examples /// /// ```gleam -/// clamp(1.2, start: 1.4, stop: 1.6) +/// clamp(1.2, from: 1.4, to: 1.6) /// // -> 1.4 /// ``` /// /// ```gleam -/// clamp(1.2, start: 1.4, stop: 0.6) +/// clamp(1.2, from: 1.4, to: 0.6) /// // -> 1.2 /// ``` /// -pub fn clamp( - x: Float, - start start_bound: Float, - stop stop_bound: Float, -) -> Float { +pub fn clamp(x: Float, from start_bound: Float, to stop_bound: Float) -> Float { case start_bound >=. stop_bound { True -> x |> min(start_bound) |> max(stop_bound) False -> x |> min(stop_bound) |> max(start_bound) diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index a980c8856..d03cd5c8c 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -282,16 +282,16 @@ pub fn to_float(x: Int) -> Float /// ## Examples /// /// ```gleam -/// clamp(40, start: 50, stop: 60) +/// clamp(40, from: 50, to: 60) /// // -> 50 /// ``` /// /// ```gleam -/// clamp(40, start: 50, stop: 30) +/// clamp(40, from: 50, to: 30) /// // -> 40 /// ``` /// -pub fn clamp(x: Int, start start_bound: Int, stop stop_bound: Int) -> Int { +pub fn clamp(x: Int, from start_bound: Int, to stop_bound: Int) -> Int { case start_bound >= stop_bound { True -> x |> min(start_bound) |> max(stop_bound) False -> x |> min(stop_bound) |> max(start_bound) diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 8ef319111..ff975949d 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -61,13 +61,13 @@ pub fn to_string_test() { } pub fn clamp_test() { - assert float.clamp(1.4, start: 1.3, stop: 1.5) == 1.4 + assert float.clamp(1.4, from: 1.3, to: 1.5) == 1.4 - assert float.clamp(1.2, start: 1.3, stop: 1.5) == 1.3 + assert float.clamp(1.2, from: 1.3, to: 1.5) == 1.3 - assert float.clamp(1.6, start: 1.3, stop: 1.5) == 1.5 + assert float.clamp(1.6, from: 1.3, to: 1.5) == 1.5 - assert float.clamp(1.2, start: 1.4, stop: 0.6) == 1.2 + assert float.clamp(1.2, from: 1.4, to: 0.6) == 1.2 } pub fn compare_test() { diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index f09e98450..fe1b7f253 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -9,15 +9,15 @@ pub fn absolute_value_test() { } pub fn clamp_test() { - assert int.clamp(40, start: 30, stop: 50) == 40 + assert int.clamp(40, from: 30, to: 50) == 40 - assert int.clamp(20, start: 30, stop: 50) == 30 + assert int.clamp(20, from: 30, to: 50) == 30 - assert int.clamp(60, start: 30, stop: 50) == 50 + assert int.clamp(60, from: 30, to: 50) == 50 - assert int.clamp(100, start: 50, stop: 30) == 50 + assert int.clamp(100, from: 50, to: 30) == 50 - assert int.clamp(40, start: 50, stop: 30) == 40 + assert int.clamp(40, from: 50, to: 30) == 40 } pub fn to_string_test() { From 371818996522c4f6e5fe0a2b24f1cb6e380971ab Mon Sep 17 00:00:00 2001 From: NNB Date: Fri, 19 Sep 2025 22:12:22 +0700 Subject: [PATCH 3/3] fix: Rename back `int.clamp` and `float.clamp` labels. --- src/gleam/float.gleam | 12 ++++++------ src/gleam/int.gleam | 12 ++++++------ test/gleam/float_test.gleam | 8 ++++---- test/gleam/int_test.gleam | 10 +++++----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index d649b8c31..dc563747b 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -68,19 +68,19 @@ pub fn to_string(x: Float) -> String /// ## Examples /// /// ```gleam -/// clamp(1.2, from: 1.4, to: 1.6) +/// clamp(1.2, min: 1.4, max: 1.6) /// // -> 1.4 /// ``` /// /// ```gleam -/// clamp(1.2, from: 1.4, to: 0.6) +/// clamp(1.2, min: 1.4, max: 0.6) /// // -> 1.2 /// ``` /// -pub fn clamp(x: Float, from start_bound: Float, to stop_bound: Float) -> Float { - case start_bound >=. stop_bound { - True -> x |> min(start_bound) |> max(stop_bound) - False -> x |> min(stop_bound) |> max(start_bound) +pub fn clamp(x: Float, min min_bound: Float, max max_bound: Float) -> Float { + case min_bound >=. max_bound { + True -> x |> min(min_bound) |> max(max_bound) + False -> x |> min(max_bound) |> max(min_bound) } } diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index d03cd5c8c..4e00fc252 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -282,19 +282,19 @@ pub fn to_float(x: Int) -> Float /// ## Examples /// /// ```gleam -/// clamp(40, from: 50, to: 60) +/// clamp(40, min: 50, max: 60) /// // -> 50 /// ``` /// /// ```gleam -/// clamp(40, from: 50, to: 30) +/// clamp(40, min: 50, max: 30) /// // -> 40 /// ``` /// -pub fn clamp(x: Int, from start_bound: Int, to stop_bound: Int) -> Int { - case start_bound >= stop_bound { - True -> x |> min(start_bound) |> max(stop_bound) - False -> x |> min(stop_bound) |> max(start_bound) +pub fn clamp(x: Int, min min_bound: Int, max max_bound: Int) -> Int { + case min_bound >= max_bound { + True -> x |> min(min_bound) |> max(max_bound) + False -> x |> min(max_bound) |> max(min_bound) } } diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index ff975949d..f5c96f7ea 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -61,13 +61,13 @@ pub fn to_string_test() { } pub fn clamp_test() { - assert float.clamp(1.4, from: 1.3, to: 1.5) == 1.4 + assert float.clamp(1.4, min: 1.3, max: 1.5) == 1.4 - assert float.clamp(1.2, from: 1.3, to: 1.5) == 1.3 + assert float.clamp(1.2, min: 1.3, max: 1.5) == 1.3 - assert float.clamp(1.6, from: 1.3, to: 1.5) == 1.5 + assert float.clamp(1.6, min: 1.3, max: 1.5) == 1.5 - assert float.clamp(1.2, from: 1.4, to: 0.6) == 1.2 + assert float.clamp(1.2, min: 1.4, max: 0.6) == 1.2 } pub fn compare_test() { diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index fe1b7f253..db5a3aa54 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -9,15 +9,15 @@ pub fn absolute_value_test() { } pub fn clamp_test() { - assert int.clamp(40, from: 30, to: 50) == 40 + assert int.clamp(40, min: 30, max: 50) == 40 - assert int.clamp(20, from: 30, to: 50) == 30 + assert int.clamp(20, min: 30, max: 50) == 30 - assert int.clamp(60, from: 30, to: 50) == 50 + assert int.clamp(60, min: 30, max: 50) == 50 - assert int.clamp(100, from: 50, to: 30) == 50 + assert int.clamp(100, min: 50, max: 30) == 50 - assert int.clamp(40, from: 50, to: 30) == 40 + assert int.clamp(40, min: 50, max: 30) == 40 } pub fn to_string_test() {