From fc5dc23aec6bd5105a85142a18371a44c9d6d1f4 Mon Sep 17 00:00:00 2001 From: NicklasXYZ <18580183+NicklasXYZ@users.noreply.github.com> Date: Tue, 13 Aug 2024 00:09:11 +0200 Subject: [PATCH] Add is_divisible/is_multiple predicate functions --- src/gleam_community/maths/predicates.gleam | 124 ++++++++++++++++++ .../maths/predicates_test.gleam | 27 ++++ 2 files changed, 151 insertions(+) diff --git a/src/gleam_community/maths/predicates.gleam b/src/gleam_community/maths/predicates.gleam index ac147a6..5784328 100644 --- a/src/gleam_community/maths/predicates.gleam +++ b/src/gleam_community/maths/predicates.gleam @@ -30,6 +30,7 @@ //// * [`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) @@ -464,3 +465,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) +}