Skip to content

Commit

Permalink
♻️ Replace error types with Nil for better DX with other error-produc…
Browse files Browse the repository at this point in the history
…ing functions.
  • Loading branch information
hayleigh-dot-dev committed Aug 17, 2024
1 parent 6d5241e commit 2bd3b76
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 626 deletions.
94 changes: 44 additions & 50 deletions src/gleam_community/maths/arithmetics.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
////<style>
//// .katex { font-size: 1.1em; }
////</style>
////
////
//// ---
////
////
//// Arithmetics: A module containing a collection of fundamental mathematical functions relating to simple arithmetics (addition, subtraction, multiplication, etc.), but also number theory.
////
////
//// * **Division functions**
//// * [`gcd`](#gcd)
//// * [`lcm`](#lcm)
Expand All @@ -40,7 +40,7 @@
//// * [`int_cumulative_sum`](#int_cumulative_sum)
//// * [`float_cumulative_product`](#float_cumulative_product)
//// * [`int_cumulative_product`](#int_cumulative_product)
////
////

import gleam/int
import gleam/list
Expand All @@ -57,7 +57,7 @@ import gleam_community/maths/piecewise
/// </a>
/// </div>
///
/// The function calculates the greatest common divisor of two integers
/// The function calculates the greatest common divisor of two integers
/// \\(x, y \in \mathbb{Z}\\). The greatest common divisor is the largest positive
/// integer that is divisible by both \\(x\\) and \\(y\\).
///
Expand All @@ -70,7 +70,7 @@ import gleam_community/maths/piecewise
/// pub fn example() {
/// arithmetics.gcd(1, 1)
/// |> should.equal(1)
///
///
/// arithmetics.gcd(100, 10)
/// |> should.equal(10)
///
Expand Down Expand Up @@ -107,24 +107,24 @@ fn do_gcd(x: Int, y: Int) -> Int {
/// </a>
/// </div>
///
///
///
/// Given two integers, \\(x\\) (dividend) and \\(y\\) (divisor), the Euclidean modulo
/// of \\(x\\) by \\(y\\), denoted as \\(x \mod y\\), is the remainder \\(r\\) of the
/// of \\(x\\) by \\(y\\), denoted as \\(x \mod y\\), is the remainder \\(r\\) of the
/// division of \\(x\\) by \\(y\\), such that:
///
///
/// \\[
/// x = q \cdot y + r \quad \text{and} \quad 0 \leq r < |y|,
/// \\]
///
///
/// where \\(q\\) is an integer that represents the quotient of the division.
///
/// The Euclidean modulo function of two numbers, is the remainder operation most
/// commonly utilized in mathematics. This differs from the standard truncating
/// modulo operation frequently employed in programming via the `%` operator.
/// Unlike the `%` operator, which may return negative results depending on the
/// divisor's sign, the Euclidean modulo function is designed to always yield a
/// The Euclidean modulo function of two numbers, is the remainder operation most
/// commonly utilized in mathematics. This differs from the standard truncating
/// modulo operation frequently employed in programming via the `%` operator.
/// Unlike the `%` operator, which may return negative results depending on the
/// divisor's sign, the Euclidean modulo function is designed to always yield a
/// positive outcome, ensuring consistency with mathematical conventions.
///
///
/// Note that like the Gleam division operator `/` this will return `0` if one of
/// the arguments is `0`.
///
Expand All @@ -138,7 +138,7 @@ fn do_gcd(x: Int, y: Int) -> Int {
/// pub fn example() {
/// arithmetics.euclidean_modulo(15, 4)
/// |> should.equal(3)
///
///
/// arithmetics.euclidean_modulo(-3, -2)
/// |> should.equal(1)
///
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
/// </a>
/// </div>
///
/// The function calculates the least common multiple of two integers
/// The function calculates the least common multiple of two integers
/// \\(x, y \in \mathbb{Z}\\). The least common multiple is the smallest positive
/// integer that has both \\(x\\) and \\(y\\) as factors.
///
Expand All @@ -181,7 +181,7 @@ pub fn int_euclidean_modulo(x: Int, y: Int) -> Int {
/// pub fn example() {
/// arithmetics.lcm(1, 1)
/// |> should.equal(1)
///
///
/// arithmetics.lcm(100, 10)
/// |> should.equal(100)
///
Expand All @@ -208,7 +208,7 @@ pub fn lcm(x: Int, y: Int) -> Int {
/// </a>
/// </div>
///
/// The function returns all the positive divisors of an integer, including the
/// The function returns all the positive divisors of an integer, including the
/// number itself.
///
/// <details>
Expand Down Expand Up @@ -260,7 +260,7 @@ fn find_divisors(n: Int) -> List(Int) {
/// </a>
/// </div>
///
/// The function returns all the positive divisors of an integer, excluding the
/// The function returns all the positive divisors of an integer, excluding the
/// number iteself.
///
/// <details>
Expand Down Expand Up @@ -362,7 +362,7 @@ pub fn float_sum(arr: List(Float), weights: option.Option(List(Float))) -> Float
/// \sum_{i=1}^n x_i
/// \\]
///
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
/// the value in the input list indexed by \\(i\\).
///
/// <details>
Expand Down Expand Up @@ -414,7 +414,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\) is
/// the value in the input list indexed by \\(i\\), while the \\(w_i \in \mathbb{R}\\)
/// are corresponding weights (\\(w_i = 1.0\\;\forall i=1...n\\) by default).
///
///
/// <details>
/// <summary>Example:</summary>
///
Expand Down Expand Up @@ -444,7 +444,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
pub fn float_product(
arr: List(Float),
weights: option.Option(List(Float)),
) -> Result(Float, String) {
) -> Result(Float, Nil) {
case arr, weights {
[], _ ->
1.0
Expand All @@ -454,22 +454,16 @@ pub fn float_product(
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|> Ok
_, option.Some(warr) -> {
let results =
list.zip(arr, warr)
|> list.map(fn(a: #(Float, Float)) -> Result(Float, String) {
pair.first(a)
|> elementary.power(pair.second(a))
})
|> result.all
case results {
Ok(prods) ->
prods
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
|> Ok
Error(msg) ->
msg
|> Error
}
list.zip(arr, warr)
|> list.map(fn(a: #(Float, Float)) -> Result(Float, Nil) {
pair.first(a)
|> elementary.power(pair.second(a))
})
|> result.all
|> result.map(fn(prods) {
prods
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
})
}
}
}
Expand All @@ -486,7 +480,7 @@ pub fn float_product(
/// \prod_{i=1}^n x_i
/// \\]
///
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
/// In the formula, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\) is
/// the value in the input list indexed by \\(i\\).
///
/// <details>
Expand Down Expand Up @@ -536,7 +530,7 @@ pub fn int_product(arr: List(Int)) -> Int {
/// \\]
///
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative sum of \\(n\\)
/// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\)
/// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{R}\\)
/// is the value in the input list indexed by \\(i\\). The value \\(v_j\\) is thus the
/// sum of the \\(1\\) to \\(j\\) first elements in the given list.
///
Expand Down Expand Up @@ -586,7 +580,7 @@ pub fn float_cumulative_sum(arr: List(Float)) -> List(Float) {
/// \\]
///
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative sum of \\(n\\)
/// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\)
/// elements. That is, \\(n\\) is the length of the list and \\(x_i \in \mathbb{Z}\\)
/// is the value in the input list indexed by \\(i\\). The value \\(v_j\\) is thus the
/// sum of the \\(1\\) to \\(j\\) first elements in the given list.
///
Expand Down Expand Up @@ -635,10 +629,10 @@ pub fn int_cumulative_sum(arr: List(Int)) -> List(Int) {
/// v_j = \prod_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
/// \\]
///
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
/// \\(n\\) elements. That is, \\(n\\) is the length of the list and
/// \\(x_i \in \mathbb{R}\\) is the value in the input list indexed by \\(i\\). The
/// value \\(v_j\\) is thus the sum of the \\(1\\) to \\(j\\) first elements in the
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
/// \\(n\\) elements. That is, \\(n\\) is the length of the list and
/// \\(x_i \in \mathbb{R}\\) is the value in the input list indexed by \\(i\\). The
/// value \\(v_j\\) is thus the sum of the \\(1\\) to \\(j\\) first elements in the
/// given list.
///
/// <details>
Expand Down Expand Up @@ -687,9 +681,9 @@ pub fn float_cumulative_product(arr: List(Float)) -> List(Float) {
/// v_j = \prod_{i=1}^j x_i \\;\\; \forall j = 1,\dots, n
/// \\]
///
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
/// \\(n\\) elements. That is, \\(n\\) is the length of the list and
/// \\(x_i \in \mathbb{Z}\\) is the value in the input list indexed by \\(i\\). The
/// In the formula, \\(v_j\\) is the \\(j\\)'th element in the cumulative product of
/// \\(n\\) elements. That is, \\(n\\) is the length of the list and
/// \\(x_i \in \mathbb{Z}\\) is the value in the input list indexed by \\(i\\). The
/// value \\(v_j\\) is thus the product of the \\(1\\) to \\(j\\) first elements in the
/// given list.
///
Expand Down
Loading

0 comments on commit 2bd3b76

Please sign in to comment.