aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConnor Schembor <connor.schembor@rutgers.edu>2020-11-05 21:28:32 -0500
committerLouis Pilfold <louis@lpil.uk>2020-11-06 08:41:52 +0000
commitc1d3cc12b920f60d06265b645daa3ff0010a8402 (patch)
treeb2b3abaf035bdb14e050d8b227a894dbd6567b0b
parent46e4a4abb591be88a633da330a344b8a96b3772a (diff)
downloadgleam_stdlib-c1d3cc12b920f60d06265b645daa3ff0010a8402.tar.gz
gleam_stdlib-c1d3cc12b920f60d06265b645daa3ff0010a8402.zip
Add product function to int and float modules
-rw-r--r--CHANGELOG.md4
-rw-r--r--src/gleam/float.gleam21
-rw-r--r--src/gleam/int.gleam21
-rw-r--r--test/gleam/float_test.gleam11
-rw-r--r--test/gleam/int_test.gleam11
5 files changed, 66 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 631b4d1..348df3f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,8 +2,8 @@
## Unreleased
-- The `int` module gains the `sum` function.
-- The `float` module gains the `sum` function.
+- The `int` module gains the `sum` and `product` functions.
+- The `float` module gains the `sum` and `product` functions.
## v0.12.0 - 2020-11-04
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 3d611b5..8018c95 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -190,3 +190,24 @@ fn do_sum(numbers: List(Float), initial: Float) -> Float {
[x, ..rest] -> do_sum(rest, x +. initial)
}
}
+
+/// Multiplies a list of Floats and returns the product.
+///
+/// ## Example
+///
+/// > product([2.5, 3.2, 4.2])
+/// 33.6
+///
+pub fn product(numbers: List(Float)) -> Float {
+ case numbers {
+ [] -> 0.
+ _ -> do_product(numbers, 1.)
+ }
+}
+
+fn do_product(numbers: List(Float), initial: Float) -> Float {
+ case numbers {
+ [] -> initial
+ [x, ..rest] -> do_product(rest, x *. initial)
+ }
+}
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 0bbd63d..002a155 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -167,3 +167,24 @@ fn do_sum(numbers: List(Int), initial: Int) -> Int {
[x, ..rest] -> do_sum(rest, x + initial)
}
}
+
+/// Multiplies a list of Ints and returns the product.
+///
+/// ## Example
+///
+/// > product([2, 3, 4])
+/// 24
+///
+pub fn product(numbers: List(Int)) -> Int {
+ case numbers {
+ [] -> 0
+ _ -> do_product(numbers, 1)
+ }
+}
+
+fn do_product(numbers: List(Int), initial: Int) -> Int {
+ case numbers {
+ [] -> initial
+ [x, ..rest] -> do_product(rest, x * initial)
+ }
+}
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index 2ebaefc..2b3334e 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -250,3 +250,14 @@ pub fn sum_test() {
float.sum([1.0, 2.2, 3.3])
|> should.equal(6.5)
}
+
+pub fn product_test() {
+ float.product([])
+ |> should.equal(0.)
+
+ float.product([4.])
+ |> should.equal(4.)
+
+ float.product([2.5, 3.2, 4.2])
+ |> should.equal(33.6)
+}
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index cd7b2b9..22a35e9 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -190,3 +190,14 @@ pub fn sum_test() {
int.sum([1, 2, 3])
|> should.equal(6)
}
+
+pub fn product_test() {
+ int.product([])
+ |> should.equal(0)
+
+ int.product([4])
+ |> should.equal(4)
+
+ int.product([1, 2, 3])
+ |> should.equal(6)
+}