mirror of
https://github.com/sigmasternchen/gleam-community-maths
synced 2025-03-15 07:59:01 +00:00
Work on int_list module
This commit is contained in:
parent
7ab5d4bcbe
commit
d4fbf0f121
2 changed files with 121 additions and 0 deletions
|
@ -42,6 +42,103 @@ import gleam/float
|
|||
import gleam/pair
|
||||
import gleam_community/maths/int as intx
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/gleam-community/maths/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// 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])
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn cumulative_sum(arr: List(Int)) -> List(Int) {
|
||||
case arr {
|
||||
[] -> []
|
||||
_ ->
|
||||
arr
|
||||
|> list.scan(0, fn(acc: Int, a: Int) -> Int { a + acc })
|
||||
}
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/gleam-community/maths/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// <details>
|
||||
/// <summary>Example:</summary>
|
||||
///
|
||||
/// 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])
|
||||
/// }
|
||||
/// </details>
|
||||
///
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="#">
|
||||
/// <small>Back to top ↑</small>
|
||||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn cumumlative_product(arr: List(Int)) -> List(Int) {
|
||||
case arr {
|
||||
[] -> []
|
||||
_ ->
|
||||
arr
|
||||
|> list.scan(1, fn(acc: Int, a: Int) -> Int { a * acc })
|
||||
}
|
||||
}
|
||||
|
||||
/// <div style="text-align: right;">
|
||||
/// <a href="https://github.com/gleam-community/maths/issues">
|
||||
/// <small>Spot a typo? Open an issue!</small>
|
||||
|
|
|
@ -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])
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue