Skip to content

Commit

Permalink
Merge branch 'main' into refactor/nil-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklasXYZ authored Oct 2, 2024
2 parents 2bd3b76 + 3254597 commit eb1b743
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 188 deletions.
36 changes: 17 additions & 19 deletions src/gleam_community/maths/arithmetics.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ import gleam_community/maths/piecewise
/// </div>
///
pub fn gcd(x: Int, y: Int) -> Int {
let absx: Int = piecewise.int_absolute_value(x)
let absy: Int = piecewise.int_absolute_value(y)
let absx = piecewise.int_absolute_value(x)
let absy = piecewise.int_absolute_value(y)
do_gcd(absx, absy)
}

Expand Down Expand Up @@ -197,8 +197,8 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
/// </div>
///
pub fn lcm(x: Int, y: Int) -> Int {
let absx: Int = piecewise.int_absolute_value(x)
let absy: Int = piecewise.int_absolute_value(y)
let absx = piecewise.int_absolute_value(x)
let absy = piecewise.int_absolute_value(y)
absx * absy / do_gcd(absx, absy)
}

Expand Down Expand Up @@ -240,11 +240,11 @@ pub fn divisors(n: Int) -> List(Int) {
}

fn find_divisors(n: Int) -> List(Int) {
let nabs: Float = piecewise.float_absolute_value(conversion.int_to_float(n))
let nabs = piecewise.float_absolute_value(conversion.int_to_float(n))
let assert Ok(sqrt_result) = elementary.square_root(nabs)
let max: Int = conversion.float_to_int(sqrt_result) + 1
let max = conversion.float_to_int(sqrt_result) + 1
list.range(2, max)
|> list.fold([1, n], fn(acc: List(Int), i: Int) -> List(Int) {
|> list.fold([1, n], fn(acc, i) {
case n % i == 0 {
True -> [i, n / i, ..acc]
False -> acc
Expand Down Expand Up @@ -288,7 +288,7 @@ fn find_divisors(n: Int) -> List(Int) {
/// </div>
///
pub fn proper_divisors(n: Int) -> List(Int) {
let divisors: List(Int) = find_divisors(n)
let divisors = find_divisors(n)
divisors
|> list.take(list.length(divisors) - 1)
}
Expand Down Expand Up @@ -340,12 +340,10 @@ pub fn float_sum(arr: List(Float), weights: option.Option(List(Float))) -> Float
[], _ -> 0.0
_, option.None ->
arr
|> list.fold(0.0, fn(acc: Float, a: Float) -> Float { a +. acc })
|> list.fold(0.0, fn(acc, a) { a +. acc })
_, option.Some(warr) -> {
list.zip(arr, warr)
|> list.fold(0.0, fn(acc: Float, a: #(Float, Float)) -> Float {
pair.first(a) *. pair.second(a) +. acc
})
|> list.fold(0.0, fn(acc, a) { pair.first(a) *. pair.second(a) +. acc })
}
}
}
Expand Down Expand Up @@ -395,7 +393,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
[] -> 0
_ ->
arr
|> list.fold(0, fn(acc: Int, a: Int) -> Int { a + acc })
|> list.fold(0, fn(acc, a) { a + acc })
}
}

Expand Down Expand Up @@ -451,7 +449,7 @@ pub fn float_product(
|> Ok
_, option.None ->
arr
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|> list.fold(1.0, fn(acc, a) { a *. acc })
|> Ok
_, option.Some(warr) -> {
list.zip(arr, warr)
Expand Down Expand Up @@ -513,7 +511,7 @@ pub fn int_product(arr: List(Int)) -> Int {
[] -> 1
_ ->
arr
|> list.fold(1, fn(acc: Int, a: Int) -> Int { a * acc })
|> list.fold(1, fn(acc, a) { a * acc })
}
}

Expand Down Expand Up @@ -563,7 +561,7 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) {
[] -> []
_ ->
arr
|> list.scan(0.0, fn(acc: Float, a: Float) -> Float { a +. acc })
|> list.scan(0.0, fn(acc, a) { a +. acc })
}
}

Expand Down Expand Up @@ -613,7 +611,7 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) {
[] -> []
_ ->
arr
|> list.scan(0, fn(acc: Int, a: Int) -> Int { a + acc })
|> list.scan(0, fn(acc, a) { a + acc })
}
}

Expand Down Expand Up @@ -665,7 +663,7 @@ pub fn float_cumulative_product(arr: List(Float)) -> List(Float) {
[] -> []
_ ->
arr
|> list.scan(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|> list.scan(1.0, fn(acc, a) { a *. acc })
}
}

Expand Down Expand Up @@ -717,6 +715,6 @@ pub fn int_cumulative_product(arr: List(Int)) -> List(Int) {
[] -> []
_ ->
arr
|> list.scan(1, fn(acc: Int, a: Int) -> Int { a * acc })
|> list.scan(1, fn(acc, a) { a * acc })
}
}
25 changes: 9 additions & 16 deletions src/gleam_community/maths/combinatorics.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn combination_without_repetitions(n: Int, k: Int) -> Result(Int, Nil) {
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, x) { acc * { n + 1 - x } / x })
|> Ok
}
}
Expand Down Expand Up @@ -206,7 +206,7 @@ pub fn factorial(n) -> Result(Int, Nil) {
1 -> Ok(1)
_ ->
list.range(1, n)
|> list.fold(1, fn(acc: Int, x: Int) -> Int { acc * x })
|> list.fold(1, fn(acc, x) { acc * x })
|> Ok
}
}
Expand Down Expand Up @@ -310,7 +310,7 @@ fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, Nil) {
}
_, _ ->
list.range(0, k - 1)
|> list.fold(1, fn(acc: Int, x: Int) -> Int { acc * { n - x } })
|> list.fold(1, fn(acc, x) { acc * { n - x } })
|> Ok
}
}
Expand Down Expand Up @@ -620,18 +620,11 @@ fn do_list_permutation_with_repetitions(
/// </a>
/// </div>
///
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(a)) -> set.Set(#(a, a)) {
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(b)) -> set.Set(#(a, b)) {
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, b)), member0: a) {
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, b)), member1: b) {
set.insert(accumulator1, #(member0, member1))
})
})
}
2 changes: 1 addition & 1 deletion src/gleam_community/maths/elementary.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ fn do_logarithm_10(a: Float) -> Float
/// </div>
///
pub fn power(x: Float, y: Float) -> Result(Float, Nil) {
let fractional: Bool = do_ceiling(y) -. y >. 0.0
let fractional = do_ceiling(y) -. y >. 0.0
// In the following check:
// 1. If the base (x) is negative and the exponent (y) is fractional
// then return an error as it will otherwise be an imaginary number
Expand Down
Loading

0 comments on commit eb1b743

Please sign in to comment.