From d4fbf0f1218592191a37be594e3a018ef6e18ef8 Mon Sep 17 00:00:00 2001 From: NicklasXYZ Date: Sat, 28 Jan 2023 21:53:44 +0100 Subject: [PATCH] Work on int_list module --- src/gleam_community/maths/int_list.gleam | 97 +++++++++++++++++++ .../gleam_community_maths_int_list_test.gleam | 24 +++++ 2 files changed, 121 insertions(+) diff --git a/src/gleam_community/maths/int_list.gleam b/src/gleam_community/maths/int_list.gleam index 99e99a2..c4439af 100644 --- a/src/gleam_community/maths/int_list.gleam +++ b/src/gleam_community/maths/int_list.gleam @@ -42,6 +42,103 @@ import gleam/float import gleam/pair import gleam_community/maths/int as intx +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// Calculcate the cumulative sum of the elements in a list: +/// +/// \\[ +/// v_j = \sum_{i=1}^j x_i, \forall j \leq n +/// \\] +/// +/// In the formula, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// Furthermore, $$v_j$$ is the $$j$$th element in the cumulative sum. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int_list +/// +/// pub fn example () { +/// [] +/// |> int_list.cumulative_sum() +/// |> should.equal([]) +/// +/// // Valid input returns a result +/// [1, 2, 3] +/// |> int_list.cumulative_sum() +/// |> should.equal([1, 3, 6]) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn cumulative_sum(arr: List(Int)) -> List(Int) { + case arr { + [] -> [] + _ -> + arr + |> list.scan(0, fn(acc: Int, a: Int) -> Int { a + acc }) + } +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// Calculcate the cumulative product of the elements in a list: +/// +/// \\[ +/// v_j = \prod_{i=1}^j x_i, \forall j \leq n +/// \\] +/// +/// In the formula, $$n$$ is the length of the list and $$x_i$$ is the value in the input list indexed by $$i$$. +/// Furthermore, $$v_j$$ is the $$j$$th element in the cumulative product. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int_list +/// +/// pub fn example () { +/// // An empty list returns an error +/// [] +/// |> int_list.cumulative_product() +/// |> should.equal([]) +/// +/// // Valid input returns a result +/// [1, 2, 3] +/// |> int_list.cumulative_product() +/// |> should.equal([1, 2, 6]) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn cumumlative_product(arr: List(Int)) -> List(Int) { + case arr { + [] -> [] + _ -> + arr + |> list.scan(1, fn(acc: Int, a: Int) -> Int { a * acc }) + } +} + ///
/// /// Spot a typo? Open an issue! diff --git a/test/gleam/gleam_community_maths_int_list_test.gleam b/test/gleam/gleam_community_maths_int_list_test.gleam index 8dfcdba..1be0e4e 100644 --- a/test/gleam/gleam_community_maths_int_list_test.gleam +++ b/test/gleam/gleam_community_maths_int_list_test.gleam @@ -68,3 +68,27 @@ pub fn int_list_extrema_test() { |> int_list.extrema() |> should.equal(Ok(#(1, 4))) } + +pub fn int_list_cumulative_sum_test() { + // An empty lists returns an empty list + [] + |> int_list.cumulative_sum() + |> should.equal([]) + + // Valid input returns a result + [1, 2, 3] + |> int_list.cumulative_sum() + |> should.equal([1, 3, 6]) +} + +pub fn int_list_cumulative_product_test() { + // An empty lists returns an empty list + [] + |> int_list.cumumlative_product() + |> should.equal([]) + + // Valid input returns a result + [1, 2, 3] + |> int_list.cumumlative_product() + |> should.equal([1, 2, 6]) +}