aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parent4d13e36b03e31ee4ea17e0e615fd215815c4b3c6 (diff)
downloadgleam_stdlib-d45d4784eeec8bd216fa569909269fca48a64740.tar.gz
gleam_stdlib-d45d4784eeec8bd216fa569909269fca48a64740.zip
Result returning division functions
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam19
-rw-r--r--src/gleam/int.gleam19
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)
+ }
+}