aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Attard <robert.attard@mail.mcgill.ca>2022-01-03 18:51:10 -0500
committerGitHub <noreply@github.com>2022-01-03 23:51:10 +0000
commit265c228f4a8f233d863f35df9f8e888918902cdc (patch)
treec7a6a35939625c1096670d927951379f9b625523 /src
parent2c7c5d2a511bd5cb6ed65214790c5a91488dd7e5 (diff)
downloadgleam_stdlib-265c228f4a8f233d863f35df9f8e888918902cdc.tar.gz
gleam_stdlib-265c228f4a8f233d863f35df9f8e888918902cdc.zip
Int.digits & int.undigits
Diffstat (limited to 'src')
-rw-r--r--src/gleam/int.gleam59
1 files changed, 58 insertions, 1 deletions
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index a9dff63..2aff67f 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -62,7 +62,7 @@ if javascript {
"../gleam_stdlib.mjs" "to_string"
}
-/// For use in `to_base_string` when base is outside of the allowed range.
+/// Error value when trying to operate with a base out of the allowed range.
pub type InvalidBase {
InvalidBase
}
@@ -323,3 +323,60 @@ fn do_product(numbers: List(Int), initial: Int) -> Int {
[x, ..rest] -> do_product(rest, x * initial)
}
}
+
+/// Splits an integer into its digit representation in the specified base
+///
+/// ## Examples
+///
+/// > digits(234, 10)
+/// Ok([2,3,4])
+///
+/// > digits(234, 1)
+/// Error(InvalidBase)
+///
+pub fn digits(number: Int, base: Int) -> Result(List(Int), InvalidBase) {
+ case base < 2 {
+ True -> Error(InvalidBase)
+ False -> Ok(do_digits(number, base, []))
+ }
+}
+
+fn do_digits(number: Int, base: Int, acc: List(Int)) -> List(Int) {
+ case absolute_value(number) < base {
+ True -> [number, ..acc]
+ False -> do_digits(number / base, base, [number % base, ..acc])
+ }
+}
+
+/// Joins a list of digits into a single value.
+/// Returns an error if the base is less than 2 or if the list contains a digit greater than or equal to the specified base.
+///
+/// ## Examples
+///
+/// > undigits([2,3,4], 10)
+/// Ok(234)
+///
+/// > undigits([2,3,4], 1)
+/// Error(InvalidBase)
+///
+/// > undigits([2,3,4], 2)
+/// Error(InvalidBase)
+///
+pub fn undigits(numbers: List(Int), base: Int) -> Result(Int, InvalidBase) {
+ case base < 2 {
+ True -> Error(InvalidBase)
+ False -> do_undigits(numbers, base, 0)
+ }
+}
+
+fn do_undigits(
+ numbers: List(Int),
+ base: Int,
+ acc: Int,
+) -> Result(Int, InvalidBase) {
+ case numbers {
+ [] -> Ok(acc)
+ [digit, .._rest] if digit >= base -> Error(InvalidBase)
+ [digit, ..rest] -> do_undigits(rest, base, acc * base + digit)
+ }
+}