Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/gleam/float.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,25 @@ 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, from: 1.4, to: 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, from: 1.4, to: 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)
}
}

/// Compares two `Float`s, returning an `Order`:
Expand Down
18 changes: 12 additions & 6 deletions src/gleam/int.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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, from: 50, to: 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, from: 50, to: 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)
}
}

/// Compares two ints, returning an order.
Expand Down
8 changes: 5 additions & 3 deletions test/gleam/float_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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, from: 1.3, to: 1.5) == 1.4

assert float.clamp(1.2, min: 1.3, max: 1.5) == 1.3
assert float.clamp(1.2, from: 1.3, to: 1.5) == 1.3

assert float.clamp(1.6, min: 1.3, max: 1.5) == 1.5
assert float.clamp(1.6, from: 1.3, to: 1.5) == 1.5

assert float.clamp(1.2, from: 1.4, to: 0.6) == 1.2
}

pub fn compare_test() {
Expand Down
11 changes: 6 additions & 5 deletions test/gleam/int_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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, from: 30, to: 50) == 40

assert int.clamp(20, min: 30, max: 50) == 30
assert int.clamp(20, from: 30, to: 50) == 30

assert int.clamp(60, min: 30, max: 50) == 50
assert int.clamp(60, from: 30, to: 50) == 50

// If the bounds are reversed we return the min
assert int.clamp(100, min: 50, max: 30) == 50
assert int.clamp(100, from: 50, to: 30) == 50

assert int.clamp(40, from: 50, to: 30) == 40
}

pub fn to_string_test() {
Expand Down