aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-05-03 20:19:34 +0000
committerGitHub <noreply@github.com>2022-05-03 21:19:34 +0100
commitd45d4784eeec8bd216fa569909269fca48a64740 (patch)
treeb9f6f01c16faf042f4b27a00d16dd078b5b1ddac
parent4d13e36b03e31ee4ea17e0e615fd215815c4b3c6 (diff)
downloadgleam_stdlib-d45d4784eeec8bd216fa569909269fca48a64740.tar.gz
gleam_stdlib-d45d4784eeec8bd216fa569909269fca48a64740.zip
Result returning division functions
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/float.gleam19
-rw-r--r--src/gleam/int.gleam19
-rw-r--r--test/gleam/float_test.gleam12
-rw-r--r--test/gleam/int_test.gleam12
5 files changed, 64 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f2be94c..b131498 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## Unreleased
+- The `float` module gains the `divide` function.
+- The `int` module gains the `divide` function.
- The `int` module gains the `power` function.
- The `int` module gains the `square_root` function.
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)
+ }
+}
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index eb07f0e..132cd69 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -355,3 +355,15 @@ pub fn random_test() {
test_mean(1_000, -100.0, 0.0, 5.0)
test_mean(1_000, 0.0, -100.0, 5.0)
}
+
+pub fn divide_test() {
+ float.divide(1.0, 1.0)
+ |> should.equal(Ok(1.0))
+ float.divide(1.0, 0.0)
+ |> should.equal(Error(Nil))
+
+ float.divide(0.0, by: 1.0)
+ |> should.equal(Ok(0.0))
+ float.divide(1.0, by: 0.0)
+ |> should.equal(Error(Nil))
+}
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 7c7ba0f..2209490 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -391,3 +391,15 @@ pub fn random_test() {
test_average(1_000, -100, 0, 5)
test_average(1_000, 0, -100, 5)
}
+
+pub fn divide_test() {
+ int.divide(1, 1)
+ |> should.equal(Ok(1))
+ int.divide(1, 0)
+ |> should.equal(Error(Nil))
+
+ int.divide(0, by: 1)
+ |> should.equal(Ok(0))
+ int.divide(1, by: 0)
+ |> should.equal(Error(Nil))
+}