Skip to content

Commit 50862d1

Browse files
inoaslpil
authored andcommitted
product-of-empty-list-of-numbers-could-return-1
1 parent 67e9cf8 commit 50862d1

File tree

5 files changed

+7
-4
lines changed

5 files changed

+7
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- The `list` module gains the `group` function.
66
- The `dynamic` module is able to decode simple JavaScript objects to maps.
77
So, the behaviour of the `field` and `object` functions are consistent.
8+
- For a given empty list as an argument `int.product` now returns `1` instead
9+
of `0` and `float.product` now returns `1.0` instead of `1.0`. This mimicks
10+
the behavior of Elixir's `Enum.product/1`.
811

912
## v0.26.0 - 2023-01-12
1013

src/gleam/float.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ fn do_sum(numbers: List(Float), initial: Float) -> Float {
427427
///
428428
pub fn product(numbers: List(Float)) -> Float {
429429
case numbers {
430-
[] -> 0.0
430+
[] -> 1.0
431431
_ -> do_product(numbers, 1.0)
432432
}
433433
}

src/gleam/int.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ fn do_sum(numbers: List(Int), initial: Int) -> Int {
465465
///
466466
pub fn product(numbers: List(Int)) -> Int {
467467
case numbers {
468-
[] -> 0
468+
[] -> 1
469469
_ -> do_product(numbers, 1)
470470
}
471471
}

test/gleam/float_test.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub fn sum_test() {
338338

339339
pub fn product_test() {
340340
float.product([])
341-
|> should.equal(0.0)
341+
|> should.equal(1.0)
342342

343343
float.product([4.0])
344344
|> should.equal(4.0)

test/gleam/int_test.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ pub fn sum_test() {
349349

350350
pub fn product_test() {
351351
int.product([])
352-
|> should.equal(0)
352+
|> should.equal(1)
353353

354354
int.product([4])
355355
|> should.equal(4)

0 commit comments

Comments
 (0)