From 5c74fa8b7d5218aaf26798d675c261d153a0906f Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Tue, 19 Dec 2023 10:10:36 +0000 Subject: [PATCH] :heavy_plus_sign: Upgrade to gleam v0.33 --- gleam.toml | 3 +- manifest.toml | 4 +- src/gleam_community/maths/arithmetics.gleam | 15 ++--- src/gleam_community/maths/combinatorics.gleam | 31 +++++----- src/gleam_community/maths/elementary.gleam | 3 +- src/gleam_community/maths/metrics.gleam | 21 +++---- src/gleam_community/maths/piecewise.gleam | 62 +++++++++---------- src/gleam_community/maths/predicates.gleam | 3 +- src/gleam_community/maths/sequences.gleam | 10 ++- src/gleam_community/maths/special.gleam | 38 +++++------- .../gleam_community_maths_combinatorics.gleam | 56 +++++++++-------- .../gleam_community_maths_piecewise.gleam | 15 +---- 12 files changed, 116 insertions(+), 145 deletions(-) diff --git a/gleam.toml b/gleam.toml index 9dc5fd7..c539de0 100644 --- a/gleam.toml +++ b/gleam.toml @@ -4,9 +4,10 @@ version = "1.0.1" licences = ["Apache-2.0"] description = "A basic maths library" repository = { type = "github", user = "gleam-community", repo = "maths" } +gleam = ">= 0.33.0" [dependencies] -gleam_stdlib = "~> 0.33" +gleam_stdlib = "~> 0.34" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 2a00d86..d0b8834 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,10 +2,10 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_stdlib", version = "0.33.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "3CEAD7B153D896499C78390B22CC968620C27500C922AED3A5DD7B536F922B25" }, + { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, { name = "gleeunit", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D3682ED8C5F9CAE1C928F2506DE91625588CC752495988CBE0F5653A42A6F334" }, ] [requirements] -gleam_stdlib = { version = "~> 0.33" } +gleam_stdlib = { version = "~> 0.34" } gleeunit = { version = "~> 1.0" } diff --git a/src/gleam_community/maths/arithmetics.gleam b/src/gleam_community/maths/arithmetics.gleam index 3e0f63a..9d1c018 100644 --- a/src/gleam_community/maths/arithmetics.gleam +++ b/src/gleam_community/maths/arithmetics.gleam @@ -176,15 +176,12 @@ fn find_divisors(n: Int) -> List(Int) { let assert Ok(sqrt_result) = elementary.square_root(nabs) let max: Int = conversion.float_to_int(sqrt_result) + 1 list.range(2, max) - |> list.fold( - [1, n], - fn(acc: List(Int), i: Int) -> List(Int) { - case n % i == 0 { - True -> [i, n / i, ..acc] - False -> acc - } - }, - ) + |> list.fold([1, n], fn(acc: List(Int), i: Int) -> List(Int) { + case n % i == 0 { + True -> [i, n / i, ..acc] + False -> acc + } + }) |> list.unique() |> list.sort(int.compare) } diff --git a/src/gleam_community/maths/combinatorics.gleam b/src/gleam_community/maths/combinatorics.gleam index ee771a1..dcdeab1 100644 --- a/src/gleam_community/maths/combinatorics.gleam +++ b/src/gleam_community/maths/combinatorics.gleam @@ -103,10 +103,9 @@ pub fn combination(n: Int, k: Int) -> Result(Int, String) { False -> n - k } list.range(1, min) - |> list.fold( - 1, - fn(acc: Int, x: Int) -> Int { acc * { n + 1 - x } / x }, - ) + |> list.fold(1, fn(acc: Int, x: Int) -> Int { + acc * { n + 1 - x } / x + }) |> Ok } } @@ -240,7 +239,8 @@ pub fn permutation(n: Int, k: Int) -> Result(Int, String) { False -> { let assert Ok(v1) = factorial(n) let assert Ok(v2) = factorial(n - k) - v1 / v2 + v1 + / v2 |> Ok } } @@ -416,17 +416,14 @@ pub fn cartesian_product(xarr: List(a), yarr: List(a)) -> List(#(a, a)) { yarr |> set.from_list() xset - |> set.fold( - set.new(), - fn(accumulator0: set.Set(#(a, a)), member0: a) -> set.Set(#(a, a)) { - set.fold( - yset, - accumulator0, - fn(accumulator1: set.Set(#(a, a)), member1: a) -> set.Set(#(a, a)) { - set.insert(accumulator1, #(member0, member1)) - }, - ) - }, - ) + |> set.fold(set.new(), fn(accumulator0: set.Set(#(a, a)), member0: a) -> set.Set( + #(a, a), + ) { + set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, a)), member1: a) -> set.Set( + #(a, a), + ) { + set.insert(accumulator1, #(member0, member1)) + }) + }) |> set.to_list() } diff --git a/src/gleam_community/maths/elementary.gleam b/src/gleam_community/maths/elementary.gleam index 1b518a4..673cd23 100644 --- a/src/gleam_community/maths/elementary.gleam +++ b/src/gleam_community/maths/elementary.gleam @@ -819,7 +819,8 @@ pub fn logarithm(x: Float, base: option.Option(Float)) -> Result(Float, String) // Apply the "change of base formula" let assert Ok(numerator) = logarithm_10(x) let assert Ok(denominator) = logarithm_10(a) - numerator /. denominator + numerator + /. denominator |> Ok } False -> diff --git a/src/gleam_community/maths/metrics.gleam b/src/gleam_community/maths/metrics.gleam index 1dab2b4..e35e336 100644 --- a/src/gleam_community/maths/metrics.gleam +++ b/src/gleam_community/maths/metrics.gleam @@ -95,14 +95,11 @@ pub fn norm(arr: List(Float), p: Float) -> Float { _ -> { let agg: Float = arr - |> list.fold( - 0.0, - fn(acc: Float, a: Float) -> Float { - let assert Ok(result) = - elementary.power(piecewise.float_absolute_value(a), p) - result +. acc - }, - ) + |> list.fold(0.0, fn(acc: Float, a: Float) -> Float { + let assert Ok(result) = + elementary.power(piecewise.float_absolute_value(a), p) + result +. acc + }) let assert Ok(result) = elementary.power(agg, 1.0 /. p) result } @@ -480,10 +477,10 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) { }) |> arithmetics.float_sum() |> fn(a: Float) -> Float { - a /. { - conversion.int_to_float(list.length(arr)) -. conversion.int_to_float( - ddof, - ) + a + /. { + conversion.int_to_float(list.length(arr)) + -. conversion.int_to_float(ddof) } } |> Ok diff --git a/src/gleam_community/maths/piecewise.gleam b/src/gleam_community/maths/piecewise.gleam index 3b40a18..ea01e10 100644 --- a/src/gleam_community/maths/piecewise.gleam +++ b/src/gleam_community/maths/piecewise.gleam @@ -445,7 +445,8 @@ fn round_ties_up(p: Float, x: Float) -> Float { let remainder: Float = xabs -. xabs_truncated case remainder { _ if remainder >=. 0.5 && x >=. 0.0 -> - float_sign(x) *. truncate_float(xabs +. 1.0) /. p + float_sign(x) *. truncate_float(xabs +. 1.0) + /. p _ -> float_sign(x) *. xabs_truncated /. p } } @@ -576,7 +577,8 @@ pub fn int_absolute_value(x: Int) -> Int { /// /// pub fn float_absolute_difference(a: Float, b: Float) -> Float { - a -. b + a + -. b |> float_absolute_value() } @@ -616,7 +618,8 @@ pub fn float_absolute_difference(a: Float, b: Float) -> Float { /// /// pub fn int_absolute_difference(a: Int, b: Int) -> Int { - a - b + a + - b |> int_absolute_value() } @@ -944,15 +947,12 @@ pub fn list_minimum( _ -> { let assert Ok(val0) = list.at(arr, 0) arr - |> list.fold( - val0, - fn(acc: a, element: a) { - case compare(element, acc) { - order.Lt -> element - _ -> acc - } - }, - ) + |> list.fold(val0, fn(acc: a, element: a) { + case compare(element, acc) { + order.Lt -> element + _ -> acc + } + }) |> Ok } } @@ -1003,15 +1003,12 @@ pub fn list_maximum( _ -> { let assert Ok(val0) = list.at(arr, 0) arr - |> list.fold( - val0, - fn(acc: a, element: a) { - case compare(acc, element) { - order.Lt -> element - _ -> acc - } - }, - ) + |> list.fold(val0, fn(acc: a, element: a) { + case compare(acc, element) { + order.Lt -> element + _ -> acc + } + }) |> Ok } } @@ -1209,19 +1206,16 @@ pub fn extrema( let assert Ok(val_max) = list.at(arr, 0) let assert Ok(val_min) = list.at(arr, 0) arr - |> list.fold( - #(val_min, val_max), - fn(acc: #(a, a), element: a) { - let first: a = pair.first(acc) - let second: a = pair.second(acc) - case compare(element, first), compare(second, element) { - order.Lt, order.Lt -> #(element, element) - order.Lt, _ -> #(element, second) - _, order.Lt -> #(first, element) - _, _ -> #(first, second) - } - }, - ) + |> list.fold(#(val_min, val_max), fn(acc: #(a, a), element: a) { + let first: a = pair.first(acc) + let second: a = pair.second(acc) + case compare(element, first), compare(second, element) { + order.Lt, order.Lt -> #(element, element) + order.Lt, _ -> #(element, second) + _, order.Lt -> #(first, element) + _, _ -> #(first, second) + } + }) |> Ok } } diff --git a/src/gleam_community/maths/predicates.gleam b/src/gleam_community/maths/predicates.gleam index f8d357c..8348e9c 100644 --- a/src/gleam_community/maths/predicates.gleam +++ b/src/gleam_community/maths/predicates.gleam @@ -98,7 +98,8 @@ fn float_absolute_value(x: Float) -> Float { } fn float_absolute_difference(a: Float, b: Float) -> Float { - a -. b + a + -. b |> float_absolute_value() } diff --git a/src/gleam_community/maths/sequences.gleam b/src/gleam_community/maths/sequences.gleam index e7c0388..9a5cb56 100644 --- a/src/gleam_community/maths/sequences.gleam +++ b/src/gleam_community/maths/sequences.gleam @@ -144,9 +144,8 @@ pub fn linear_space( case endpoint { True -> { let increment: Float = - piecewise.float_absolute_value(start -. stop) /. conversion.int_to_float( - num - 1, - ) + piecewise.float_absolute_value(start -. stop) + /. conversion.int_to_float(num - 1) list.range(0, num - 1) |> list.map(fn(i: Int) -> Float { start +. conversion.int_to_float(i) *. increment *. direction @@ -155,9 +154,8 @@ pub fn linear_space( } False -> { let increment: Float = - piecewise.float_absolute_value(start -. stop) /. conversion.int_to_float( - num, - ) + piecewise.float_absolute_value(start -. stop) + /. conversion.int_to_float(num) list.range(0, num - 1) |> list.map(fn(i: Int) -> Float { start +. conversion.int_to_float(i) *. increment *. direction diff --git a/src/gleam_community/maths/special.gleam b/src/gleam_community/maths/special.gleam index dfd9cbb..12e2f87 100644 --- a/src/gleam_community/maths/special.gleam +++ b/src/gleam_community/maths/special.gleam @@ -87,9 +87,10 @@ pub fn erf(x: Float) -> Float { // Formula 7.1.26 given in Abramowitz and Stegun. let t: Float = 1.0 /. { 1.0 +. p *. x } let y: Float = - 1.0 -. { { { { a5 *. t +. a4 } *. t +. a3 } *. t +. a2 } *. t +. a1 } *. t *. elementary.exponential( - -1.0 *. x *. x, - ) + 1.0 + -. { { { { a5 *. t +. a4 } *. t +. a3 } *. t +. a2 } *. t +. a1 } + *. t + *. elementary.exponential(-1.0 *. x *. x) sign *. y } @@ -126,22 +127,17 @@ const lanczos_p: List(Float) = [ fn gamma_lanczos(x: Float) -> Float { case x <. 0.5 { True -> - elementary.pi() /. { - elementary.sin(elementary.pi() *. x) *. gamma_lanczos(1.0 -. x) - } + elementary.pi() + /. { elementary.sin(elementary.pi() *. x) *. gamma_lanczos(1.0 -. x) } False -> { let z = x -. 1.0 let x: Float = - list.index_fold( - lanczos_p, - 0.0, - fn(acc: Float, v: Float, index: Int) { - case index > 0 { - True -> acc +. v /. { z +. conversion.int_to_float(index) } - False -> v - } - }, - ) + list.index_fold(lanczos_p, 0.0, fn(acc: Float, v: Float, index: Int) { + case index > 0 { + True -> acc +. v /. { z +. conversion.int_to_float(index) } + False -> v + } + }) let t: Float = z +. lanczos_g +. 0.5 let assert Ok(v1) = elementary.power(2.0 *. elementary.pi(), 0.5) let assert Ok(v2) = elementary.power(t, z +. 0.5) @@ -171,13 +167,9 @@ pub fn incomplete_gamma(a: Float, x: Float) -> Result(Float, String) { case a >. 0.0 && x >=. 0.0 { True -> { let assert Ok(v) = elementary.power(x, a) - v *. elementary.exponential(-1.0 *. x) *. incomplete_gamma_sum( - a, - x, - 1.0 /. a, - 0.0, - 1.0, - ) + v + *. elementary.exponential(-1.0 *. x) + *. incomplete_gamma_sum(a, x, 1.0 /. a, 0.0, 1.0) |> Ok } diff --git a/test/gleam/gleam_community_maths_combinatorics.gleam b/test/gleam/gleam_community_maths_combinatorics.gleam index 70267ba..052ff1b 100644 --- a/test/gleam/gleam_community_maths_combinatorics.gleam +++ b/test/gleam/gleam_community_maths_combinatorics.gleam @@ -80,27 +80,26 @@ pub fn list_cartesian_product_test() { [1, 2, 3] |> combinatorics.cartesian_product([1, 2, 3]) |> set.from_list() - |> should.equal(set.from_list([ - #(1, 1), - #(1, 2), - #(1, 3), - #(2, 1), - #(2, 2), - #(2, 3), - #(3, 1), - #(3, 2), - #(3, 3), - ])) + |> should.equal( + set.from_list([ + #(1, 1), + #(1, 2), + #(1, 3), + #(2, 1), + #(2, 2), + #(2, 3), + #(3, 1), + #(3, 2), + #(3, 3), + ]), + ) [1.0, 10.0] |> combinatorics.cartesian_product([1.0, 2.0]) |> set.from_list() - |> should.equal(set.from_list([ - #(1.0, 1.0), - #(1.0, 2.0), - #(10.0, 1.0), - #(10.0, 2.0), - ])) + |> should.equal( + set.from_list([#(1.0, 1.0), #(1.0, 2.0), #(10.0, 1.0), #(10.0, 2.0)]), + ) } pub fn list_permutation_test() { @@ -125,14 +124,16 @@ pub fn list_permutation_test() { [1, 2, 3] |> combinatorics.list_permutation() |> set.from_list() - |> should.equal(set.from_list([ - [1, 2, 3], - [2, 1, 3], - [3, 1, 2], - [1, 3, 2], - [2, 3, 1], - [3, 2, 1], - ])) + |> should.equal( + set.from_list([ + [1, 2, 3], + [2, 1, 3], + [3, 1, 2], + [1, 3, 2], + [2, 3, 1], + [3, 2, 1], + ]), + ) // Repeated elements are treated as distinct for the // purpose of permutations, so two identical elements @@ -178,8 +179,9 @@ pub fn list_combination_test() { let assert Ok(result) = combinatorics.list_combination([1, 2, 3, 4], 2) result |> set.from_list() - |> should.equal(set.from_list([[1, - 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])) + |> should.equal( + set.from_list([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]), + ) // Test with some arbitrary inputs let assert Ok(result) = combinatorics.list_combination([1, 2, 3, 4], 3) diff --git a/test/gleam/gleam_community_maths_piecewise.gleam b/test/gleam/gleam_community_maths_piecewise.gleam index c116e88..e427700 100644 --- a/test/gleam/gleam_community_maths_piecewise.gleam +++ b/test/gleam/gleam_community_maths_piecewise.gleam @@ -316,26 +316,17 @@ pub fn math_round_ties_away_test() { |> should.equal(Ok(12.0)) // Round 1. digit BEFORE decimal point - piecewise.round( - 12.0654, - option.Some(-1), - option.Some(piecewise.RoundTiesAway), + piecewise.round(12.0654, option.Some(-1), option.Some(piecewise.RoundTiesAway), ) |> should.equal(Ok(10.0)) // Round 2. digit BEFORE decimal point - piecewise.round( - 12.0654, - option.Some(-2), - option.Some(piecewise.RoundTiesAway), + piecewise.round(12.0654, option.Some(-2), option.Some(piecewise.RoundTiesAway), ) |> should.equal(Ok(0.0)) // Round 2. digit BEFORE decimal point - piecewise.round( - 12.0654, - option.Some(-3), - option.Some(piecewise.RoundTiesAway), + piecewise.round(12.0654, option.Some(-3), option.Some(piecewise.RoundTiesAway), ) |> should.equal(Ok(0.0)) }