Skip to content

Commit

Permalink
Merge pull request #25 from NicklasXYZ/main
Browse files Browse the repository at this point in the history
Add new predicate functions + update workflow
  • Loading branch information
NicklasXYZ authored Aug 12, 2024
2 parents 43e5eec + b1b5ec0 commit 7fd917f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: erlef/setup-beam@v1
with:
otp-version: "26.2"
gleam-version: "1.0.0"
gleam-version: "1.4.1"

- uses: actions/setup-node@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: erlef/setup-beam@v1
with:
otp-version: "26.2"
gleam-version: "1.0.0"
gleam-version: "1.4.1"

- uses: actions/setup-node@v3
with:
Expand Down
126 changes: 126 additions & 0 deletions src/gleam_community/maths/predicates.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
//// * [`is_close`](#is_close)
//// * [`list_all_close`](#all_close)
//// * [`is_fractional`](#is_fractional)
//// * [`is_between`](#is_between)
//// * [`is_power`](#is_power)
//// * [`is_perfect`](#is_perfect)
//// * [`is_even`](#is_even)
//// * [`is_odd`](#is_odd)
//// * [`is_divisible`](#is_divisible)
//// * [`is_multiple`](#is_multiple)
//// * [`is_prime`](#is_prime)
////

Expand Down Expand Up @@ -464,3 +467,126 @@ fn powmod_with_check(base: Int, exponent: Int, modulus: Int) -> Int {
_, _ -> { base * powmod_with_check(base, exponent - 1, modulus) } % modulus
}
}

/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// A function that tests whether a given real number $$x \in \mathbb{R}$$ is strictly
/// between two other real numbers, $$a,b \in \mathbb{R}$$, such that $$a < x < b$$.
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam_community/maths/predicates
///
/// pub fn example() {
/// predicates.is_between(5.5, 5.0, 6.0)
/// |> should.equal(True)
///
/// predicates.is_between(5.0, 5.0, 6.0)
/// |> should.equal(False)
///
/// predicates.is_between(6.0, 5.0, 6.0)
/// |> should.equal(False)
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn is_between(x: Float, lower: Float, upper: Float) -> Bool {
lower <. x && x <. upper
}

/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// A function that tests whether a given integer $$n \in \mathbb{Z}$$ is divisible by another
/// integer $$d \in \mathbb{Z}$$, such that $$n \mod d = 0$$.
///
/// <details>
/// <summary>Details</summary>
///
/// For example:
/// - $$n = 10$$ is divisible by $$d = 2$$ because $$10 \mod 2 = 0$$.
/// - $$n = 7$$ is not divisible by $$d = 3$$ because $$7 \mod 3 \neq 0$$.
///
/// </details>
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam_community/maths/predicates
///
/// pub fn example() {
/// predicates.is_divisible(10, 2)
/// |> should.equal(True)
///
/// predicates.is_divisible(7, 3)
/// |> should.equal(False)
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn is_divisible(n: Int, d: Int) -> Bool {
n % d == 0
}

/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// A function that tests whether a given integer $$m \in \mathbb{Z}$$ is a multiple of another
/// integer $$k \in \mathbb{Z}$$, such that $$m = k \times q \quad q \in \mathbb{Z}$$.
///
/// <details>
/// <summary>Details</summary>
///
/// For example:
/// - $$m = 15$$ is a multiple of $$k = 5$$ because $$15 = 5 \times 3$$.
/// - $$m = 14$$ is not a multiple of $$k = 5$$ because $$14 \div 5$$ does not yield an integer quotient.
///
/// </details>
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam_community/maths/predicates
///
/// pub fn example() {
/// predicates.is_multiple(15, 5)
/// |> should.equal(True)
///
/// predicates.is_multiple(14, 5)
/// |> should.equal(False)
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn is_multiple(m: Int, k: Int) -> Bool {
m % k == 0
}
27 changes: 27 additions & 0 deletions test/gleam_community/maths/predicates_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,30 @@ pub fn int_is_prime_test() {
predicates.is_prime(1105)
|> should.equal(False)
}

pub fn is_between_test() {
predicates.is_between(5.5, 5.0, 6.0)
|> should.equal(True)

predicates.is_between(5.0, 5.0, 6.0)
|> should.equal(False)

predicates.is_between(6.0, 5.0, 6.0)
|> should.equal(False)
}

pub fn is_divisible_test() {
predicates.is_divisible(10, 2)
|> should.equal(True)

predicates.is_divisible(7, 3)
|> should.equal(False)
}

pub fn is_multiple_test() {
predicates.is_multiple(15, 5)
|> should.equal(True)

predicates.is_multiple(14, 5)
|> should.equal(False)
}

0 comments on commit 7fd917f

Please sign in to comment.