diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 19 | ||||
-rw-r--r-- | src/gleam/int.gleam | 19 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 0dd8e07..991030c 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -401,3 +401,22 @@ if javascript { external fn do_random_uniform() -> Float = "../gleam_stdlib.mjs" "random_uniform" } + +/// Returns division of the inputs as a `Result`. +/// +/// ## Examples +/// +/// ```gleam +/// > divide(0.0, 1.0) +/// Ok(1.0) +/// +/// > divide(1.0, 0.0) +/// Error(Nil) +/// ``` +/// +pub fn divide(a: Float, by b: Float) -> Result(Float, Nil) { + case b { + 0.0 -> Error(Nil) + b -> Ok(a /. b) + } +} diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 0ccaf0d..56ac399 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -498,3 +498,22 @@ pub fn random(boundary_a: Int, boundary_b: Int) -> Int { |> float.floor() |> float.round() } + +/// Returns division of the inputs as a `Result`. +/// +/// ## Examples +/// +/// ```gleam +/// > divide(0, 1) +/// Ok(1) +/// +/// > divide(1, 0) +/// Error(Nil) +/// ``` +/// +pub fn divide(a: Int, by b: Int) -> Result(Int, Nil) { + case b { + 0 -> Error(Nil) + b -> Ok(a / b) + } +} |