diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3ce4599..bc94906 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 98c5e59..e8e6d9d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: diff --git a/src/gleam_community/maths/predicates.gleam b/src/gleam_community/maths/predicates.gleam index ac147a6..940f3a9 100644 --- a/src/gleam_community/maths/predicates.gleam +++ b/src/gleam_community/maths/predicates.gleam @@ -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) //// @@ -464,3 +467,126 @@ fn powmod_with_check(base: Int, exponent: Int, modulus: Int) -> Int { _, _ -> { base * powmod_with_check(base, exponent - 1, modulus) } % modulus } } + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// 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$$. +/// +///
+/// Example: +/// +/// 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) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn is_between(x: Float, lower: Float, upper: Float) -> Bool { + lower <. x && x <. upper +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// 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 +/// +/// 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$$. +/// +///
+/// +///
+/// Example: +/// +/// 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) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn is_divisible(n: Int, d: Int) -> Bool { + n % d == 0 +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// 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 +/// +/// 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. +/// +///
+/// +///
+/// Example: +/// +/// 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) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn is_multiple(m: Int, k: Int) -> Bool { + m % k == 0 +} diff --git a/test/gleam_community/maths/predicates_test.gleam b/test/gleam_community/maths/predicates_test.gleam index 2244a9a..fcae19c 100644 --- a/test/gleam_community/maths/predicates_test.gleam +++ b/test/gleam_community/maths/predicates_test.gleam @@ -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) +}