Add trim func tests. Begin combinatorial funcs impl

This commit is contained in:
NicklasXYZ 2023-02-03 22:00:47 +01:00
parent 26d7fddc57
commit 7fbdfe1391
6 changed files with 114 additions and 4 deletions

View file

@ -54,6 +54,7 @@ import gleam/option
import gleam_community/maths/float as floatx
import gleam_community/maths/int as intx
import gleam/io
import gleam/iterator
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
@ -645,7 +646,7 @@ pub fn sum(arr: List(Float)) -> Float {
///
pub fn product(arr: List(Float)) -> Float {
case arr {
[] -> 0.0
[] -> 1.0
_ ->
arr
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })

View file

@ -192,7 +192,7 @@ pub fn sum(arr: List(Int)) -> Int {
///
pub fn product(arr: List(Int)) -> Int {
case arr {
[] -> 0
[] -> 1
_ ->
arr
|> list.fold(1, fn(acc: Int, a: Int) -> Int { a * acc })

View file

@ -81,3 +81,90 @@ pub fn trim(arr: List(a), min: Int, max: Int) -> Result(List(a), String) {
}
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Generate all $$k$$-combinations based on a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam/list
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
pub fn combination(arr: List(a), k: Int) -> List(a) {
todo
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Generate all permutations based on a given list.
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam/list
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
pub fn permutation(arr: List(a)) -> List(a) {
todo
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Generate a list containing all combinations of one element from each of two given lists.
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam/list
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
pub fn cartesian_product(xarr: List(a), yarr: List(a)) -> List(a) {
todo
}

View file

@ -481,7 +481,7 @@ pub fn float_list_product_test() {
// An empty list returns 0
[]
|> float_list.product()
|> should.equal(0.0)
|> should.equal(1.0)
// Valid input returns a result
[1.0, 2.0, 3.0]

View file

@ -105,7 +105,7 @@ pub fn int_list_product_test() {
// An empty list returns 0
[]
|> int_list.product()
|> should.equal(0)
|> should.equal(1)
// Valid input returns a result
[1, 2, 3]

View file

@ -0,0 +1,22 @@
import gleam/int
import gleam/list
import gleam/pair
import gleam_community/maths/list as listx
import gleeunit
import gleeunit/should
pub fn main() {
gleeunit.main()
}
pub fn list_trim_test() {
// An empty lists returns an error
[]
|> listx.trim(0, 0)
|> should.be_error()
// Trim the list to only the middle part of list
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
|> listx.trim(1, 4)
|> should.equal(Ok([2.0, 3.0, 4.0, 5.0]))
}