mirror of
https://github.com/sigmasternchen/gleam-community-maths
synced 2025-03-15 07:59:01 +00:00
♻️ Remove type annotations from lambdas.
This commit is contained in:
parent
4806e56baa
commit
00ad62800f
8 changed files with 101 additions and 119 deletions
|
@ -244,7 +244,7 @@ fn find_divisors(n: Int) -> List(Int) {
|
|||
let assert Ok(sqrt_result) = elementary.square_root(nabs)
|
||||
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
|
||||
|
@ -340,10 +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 {
|
||||
|> list.fold(0.0, fn(acc: Float, a) {
|
||||
pair.first(a) *. pair.second(a) +. acc
|
||||
})
|
||||
}
|
||||
|
@ -395,7 +395,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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -451,12 +451,12 @@ 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) -> {
|
||||
let results =
|
||||
list.zip(arr, warr)
|
||||
|> list.map(fn(a: #(Float, Float)) -> Result(Float, String) {
|
||||
|> list.map(fn(a) {
|
||||
pair.first(a)
|
||||
|> elementary.power(pair.second(a))
|
||||
})
|
||||
|
@ -464,7 +464,7 @@ pub fn float_product(
|
|||
case results {
|
||||
Ok(prods) ->
|
||||
prods
|
||||
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|
||||
|> list.fold(1.0, fn(acc, a) { a *. acc })
|
||||
|> Ok
|
||||
Error(msg) ->
|
||||
msg
|
||||
|
@ -519,7 +519,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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -569,7 +569,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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -619,7 +619,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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -671,7 +671,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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -723,6 +723,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 })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -161,7 +161,7 @@ fn combination_without_repetitions(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, x) { acc * { n + 1 - x } / x })
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
|
@ -215,7 +215,7 @@ pub fn factorial(n) -> Result(Int, String) {
|
|||
|> Ok
|
||||
_ ->
|
||||
list.range(1, n)
|
||||
|> list.fold(1, fn(acc: Int, x: Int) -> Int { acc * x })
|
||||
|> list.fold(1, fn(acc, x) { acc * x })
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
|
@ -325,7 +325,7 @@ fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
|
|||
}
|
||||
_, _ ->
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -654,16 +654,9 @@ fn do_list_permutation_with_repetitions(
|
|||
///
|
||||
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(a)) -> set.Set(#(a, a)) {
|
||||
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.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, a)), member1: a) {
|
||||
set.insert(accumulator1, #(member0, member1))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -182,7 +182,7 @@ pub fn norm(
|
|||
_, option.None -> {
|
||||
let aggregate =
|
||||
arr
|
||||
|> list.fold(0.0, fn(accumulator: Float, element: Float) -> Float {
|
||||
|> list.fold(0.0, fn(accumulator, element) {
|
||||
let assert Ok(result) =
|
||||
piecewise.float_absolute_value(element)
|
||||
|> elementary.power(p)
|
||||
|
@ -202,19 +202,16 @@ pub fn norm(
|
|||
let tuples = list.zip(arr, warr)
|
||||
let aggregate =
|
||||
tuples
|
||||
|> list.fold(
|
||||
0.0,
|
||||
fn(accumulator: Float, tuple: #(Float, Float)) -> Float {
|
||||
let first_element = pair.first(tuple)
|
||||
let second_element = pair.second(tuple)
|
||||
let assert Ok(result) =
|
||||
elementary.power(
|
||||
piecewise.float_absolute_value(first_element),
|
||||
p,
|
||||
)
|
||||
second_element *. result +. accumulator
|
||||
},
|
||||
)
|
||||
|> list.fold(0.0, fn(accumulator, tuple) {
|
||||
let first_element = pair.first(tuple)
|
||||
let second_element = pair.second(tuple)
|
||||
let assert Ok(result) =
|
||||
elementary.power(
|
||||
piecewise.float_absolute_value(first_element),
|
||||
p,
|
||||
)
|
||||
second_element *. result +. accumulator
|
||||
})
|
||||
let assert Ok(result) = elementary.power(aggregate, 1.0 /. p)
|
||||
result
|
||||
|> Ok
|
||||
|
@ -370,9 +367,7 @@ pub fn minkowski_distance(
|
|||
False -> {
|
||||
let differences =
|
||||
list.zip(xarr, yarr)
|
||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
||||
pair.first(tuple) -. pair.second(tuple)
|
||||
})
|
||||
|> list.map(fn(tuple) { pair.first(tuple) -. pair.second(tuple) })
|
||||
|
||||
let assert Ok(result) = norm(differences, p, weights)
|
||||
result
|
||||
|
@ -496,7 +491,7 @@ pub fn chebyshev_distance(
|
|||
|> Error
|
||||
Ok(_) -> {
|
||||
list.zip(xarr, yarr)
|
||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
||||
|> list.map(fn(tuple) {
|
||||
{ pair.first(tuple) -. pair.second(tuple) }
|
||||
|> piecewise.float_absolute_value()
|
||||
})
|
||||
|
@ -553,9 +548,7 @@ pub fn mean(arr: List(Float)) -> Result(Float, String) {
|
|||
_ ->
|
||||
arr
|
||||
|> arithmetics.float_sum(option.None)
|
||||
|> fn(a: Float) -> Float {
|
||||
a /. conversion.int_to_float(list.length(arr))
|
||||
}
|
||||
|> fn(a) { a /. conversion.int_to_float(list.length(arr)) }
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
|
@ -684,12 +677,12 @@ pub fn variance(arr: List(Float), ddof: Int) -> Result(Float, String) {
|
|||
False -> {
|
||||
let assert Ok(mean) = mean(arr)
|
||||
arr
|
||||
|> list.map(fn(a: Float) -> Float {
|
||||
|> list.map(fn(a) {
|
||||
let assert Ok(result) = elementary.power(a -. mean, 2.0)
|
||||
result
|
||||
})
|
||||
|> arithmetics.float_sum(option.None)
|
||||
|> fn(a: Float) -> Float {
|
||||
|> fn(a) {
|
||||
a
|
||||
/. {
|
||||
conversion.int_to_float(list.length(arr))
|
||||
|
@ -1094,9 +1087,7 @@ pub fn cosine_similarity(
|
|||
|
||||
let numerator_elements =
|
||||
zipped_arr
|
||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
||||
pair.first(tuple) *. pair.second(tuple)
|
||||
})
|
||||
|> list.map(fn(tuple) { pair.first(tuple) *. pair.second(tuple) })
|
||||
|
||||
case weights {
|
||||
option.None -> {
|
||||
|
@ -1284,14 +1275,14 @@ pub fn braycurtis_distance(
|
|||
let zipped_arr = list.zip(xarr, yarr)
|
||||
let numerator_elements =
|
||||
zipped_arr
|
||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
||||
|> list.map(fn(tuple) {
|
||||
piecewise.float_absolute_value({
|
||||
pair.first(tuple) -. pair.second(tuple)
|
||||
})
|
||||
})
|
||||
let denominator_elements =
|
||||
zipped_arr
|
||||
|> list.map(fn(tuple: #(Float, Float)) -> Float {
|
||||
|> list.map(fn(tuple) {
|
||||
piecewise.float_absolute_value({
|
||||
pair.first(tuple) +. pair.second(tuple)
|
||||
})
|
||||
|
|
|
@ -823,7 +823,7 @@ pub fn int_flip_sign(x: Int) -> Int {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
||||
pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) {
|
||||
case compare(x, y) {
|
||||
order.Lt -> x
|
||||
order.Eq -> x
|
||||
|
@ -869,7 +869,7 @@ pub fn minimum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
||||
pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) {
|
||||
case compare(x, y) {
|
||||
order.Lt -> y
|
||||
order.Eq -> y
|
||||
|
@ -909,7 +909,7 @@ pub fn maximum(x: a, y: a, compare: fn(a, a) -> order.Order) -> a {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) -> #(a, a) {
|
||||
pub fn minmax(x: a, y: a, compare: fn(a, a) -> order.Order) {
|
||||
#(minimum(x, y, compare), maximum(x, y, compare))
|
||||
}
|
||||
|
||||
|
@ -956,7 +956,7 @@ pub fn list_minimum(
|
|||
|> Error
|
||||
[x, ..rest] ->
|
||||
Ok(
|
||||
list.fold(rest, x, fn(acc: a, element: a) {
|
||||
list.fold(rest, x, fn(acc, element) {
|
||||
case compare(element, acc) {
|
||||
order.Lt -> element
|
||||
_ -> acc
|
||||
|
@ -1010,7 +1010,7 @@ pub fn list_maximum(
|
|||
|> Error
|
||||
[x, ..rest] ->
|
||||
Ok(
|
||||
list.fold(rest, x, fn(acc: a, element: a) {
|
||||
list.fold(rest, x, fn(acc, element) {
|
||||
case compare(acc, element) {
|
||||
order.Lt -> element
|
||||
_ -> acc
|
||||
|
@ -1073,13 +1073,13 @@ pub fn arg_minimum(
|
|||
arr
|
||||
|> list_minimum(compare)
|
||||
arr
|
||||
|> list.index_map(fn(element: a, index: Int) -> Int {
|
||||
|> list.index_map(fn(element, index) {
|
||||
case compare(element, min) {
|
||||
order.Eq -> index
|
||||
_ -> -1
|
||||
}
|
||||
})
|
||||
|> list.filter(fn(index: Int) -> Bool {
|
||||
|> list.filter(fn(index) {
|
||||
case index {
|
||||
-1 -> False
|
||||
_ -> True
|
||||
|
@ -1143,13 +1143,13 @@ pub fn arg_maximum(
|
|||
arr
|
||||
|> list_maximum(compare)
|
||||
arr
|
||||
|> list.index_map(fn(element: a, index: Int) -> Int {
|
||||
|> list.index_map(fn(element, index) {
|
||||
case compare(element, max) {
|
||||
order.Eq -> index
|
||||
_ -> -1
|
||||
}
|
||||
})
|
||||
|> list.filter(fn(index: Int) -> Bool {
|
||||
|> list.filter(fn(index) {
|
||||
case index {
|
||||
-1 -> False
|
||||
_ -> True
|
||||
|
@ -1210,7 +1210,7 @@ pub fn extrema(
|
|||
|> Error
|
||||
[x, ..rest] ->
|
||||
Ok(
|
||||
list.fold(rest, #(x, x), fn(acc: #(a, a), element: a) {
|
||||
list.fold(rest, #(x, x), fn(acc, element) {
|
||||
let first = pair.first(acc)
|
||||
let second = pair.second(acc)
|
||||
case compare(element, first), compare(second, element) {
|
||||
|
|
|
@ -135,11 +135,11 @@ fn float_absolute_difference(a: Float, b: Float) -> Float {
|
|||
/// let rtol = 0.01
|
||||
/// let atol = 0.10
|
||||
/// predicates.all_close(xarr, yarr, rtol, atol)
|
||||
/// |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
|
||||
/// |> fn(zarr), String)) {
|
||||
/// case zarr {
|
||||
/// Ok(arr) ->
|
||||
/// arr
|
||||
/// |> list.all(fn(a: Bool) -> Bool { a })
|
||||
/// |> list.all(fn(a) { a })
|
||||
/// |> Ok
|
||||
/// _ -> Nil |> Error
|
||||
/// }
|
||||
|
@ -168,9 +168,7 @@ pub fn all_close(
|
|||
|> Error
|
||||
True ->
|
||||
list.zip(xarr, yarr)
|
||||
|> list.map(fn(z: #(Float, Float)) -> Bool {
|
||||
is_close(pair.first(z), pair.second(z), rtol, atol)
|
||||
})
|
||||
|> list.map(fn(z) { is_close(pair.first(z), pair.second(z), rtol, atol) })
|
||||
|> Ok
|
||||
}
|
||||
}
|
||||
|
@ -304,7 +302,7 @@ fn do_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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ pub fn arange(
|
|||
|> conversion.float_to_int()
|
||||
|
||||
iterator.range(0, num - 1)
|
||||
|> iterator.map(fn(i: Int) {
|
||||
|> iterator.map(fn(i) {
|
||||
start +. conversion.int_to_float(i) *. step_abs *. direction
|
||||
})
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ pub fn linear_space(
|
|||
case num > 0 {
|
||||
True -> {
|
||||
iterator.range(0, num - 1)
|
||||
|> iterator.map(fn(i: Int) -> Float {
|
||||
|> iterator.map(fn(i) {
|
||||
start +. conversion.int_to_float(i) *. increment *. direction
|
||||
})
|
||||
|> Ok
|
||||
|
@ -246,7 +246,7 @@ pub fn logarithmic_space(
|
|||
True -> {
|
||||
let assert Ok(linspace) = linear_space(start, stop, num, endpoint)
|
||||
linspace
|
||||
|> iterator.map(fn(i: Float) -> Float {
|
||||
|> iterator.map(fn(i) {
|
||||
let assert Ok(result) = elementary.power(base, i)
|
||||
result
|
||||
})
|
||||
|
|
|
@ -132,7 +132,7 @@ fn gamma_lanczos(x: Float) -> Float {
|
|||
False -> {
|
||||
let z = x -. 1.0
|
||||
let x =
|
||||
list.index_fold(lanczos_p, 0.0, fn(acc: Float, v: Float, index: Int) {
|
||||
list.index_fold(lanczos_p, 0.0, fn(acc, v, index) {
|
||||
case index > 0 {
|
||||
True -> acc +. v /. { z +. conversion.int_to_float(index) }
|
||||
False -> v
|
||||
|
|
|
@ -23,11 +23,11 @@ pub fn float_list_all_close_test() {
|
|||
let rtol = 0.01
|
||||
let atol = 0.1
|
||||
predicates.all_close(xarr, yarr, rtol, atol)
|
||||
|> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
|
||||
|> fn(zarr) {
|
||||
case zarr {
|
||||
Ok(arr) ->
|
||||
arr
|
||||
|> list.all(fn(a: Bool) -> Bool { a })
|
||||
|> list.all(fn(a) { a })
|
||||
|> Ok
|
||||
_ ->
|
||||
Nil
|
||||
|
|
Loading…
Reference in a new issue