aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2023-01-31 15:02:24 +0100
committerLouis Pilfold <louis@lpil.uk>2023-02-01 17:29:12 +0000
commit50862d1acc5c74fbf8de2b46f35c17c9a49057a4 (patch)
tree7c223d9114484c05293cb4286cafc089e55e2c9d
parent67e9cf8ea21ac829612e6e0ce2488c4748667a2a (diff)
downloadgleam_stdlib-50862d1acc5c74fbf8de2b46f35c17c9a49057a4.tar.gz
gleam_stdlib-50862d1acc5c74fbf8de2b46f35c17c9a49057a4.zip
product-of-empty-list-of-numbers-could-return-1
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/gleam/float.gleam2
-rw-r--r--src/gleam/int.gleam2
-rw-r--r--test/gleam/float_test.gleam2
-rw-r--r--test/gleam/int_test.gleam2
5 files changed, 7 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e7251eb..8454f01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,9 @@
- The `list` module gains the `group` function.
- The `dynamic` module is able to decode simple JavaScript objects to maps.
So, the behaviour of the `field` and `object` functions are consistent.
+- For a given empty list as an argument `int.product` now returns `1` instead
+ of `0` and `float.product` now returns `1.0` instead of `1.0`. This mimicks
+ the behavior of Elixir's `Enum.product/1`.
## v0.26.0 - 2023-01-12
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 5893b44..1ee175c 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -427,7 +427,7 @@ fn do_sum(numbers: List(Float), initial: Float) -> Float {
///
pub fn product(numbers: List(Float)) -> Float {
case numbers {
- [] -> 0.0
+ [] -> 1.0
_ -> do_product(numbers, 1.0)
}
}
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index eafbade..7623331 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -465,7 +465,7 @@ fn do_sum(numbers: List(Int), initial: Int) -> Int {
///
pub fn product(numbers: List(Int)) -> Int {
case numbers {
- [] -> 0
+ [] -> 1
_ -> do_product(numbers, 1)
}
}
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index 49e4261..e3278d5 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -338,7 +338,7 @@ pub fn sum_test() {
pub fn product_test() {
float.product([])
- |> should.equal(0.0)
+ |> should.equal(1.0)
float.product([4.0])
|> should.equal(4.0)
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 608f77f..bd43324 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -349,7 +349,7 @@ pub fn sum_test() {
pub fn product_test() {
int.product([])
- |> should.equal(0)
+ |> should.equal(1)
int.product([4])
|> should.equal(4)