aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGiacomo Cavalieri <giacomo.cavalieri@icloud.com>2024-08-11 16:24:46 +0200
committerLouis Pilfold <louis@lpil.uk>2024-08-13 19:31:45 +0200
commite174d4eea80dcf28f72c30d78cc17493998687aa (patch)
tree6122541ee80a1a043c0659080da73f66c667c88d /src
parent94b80cd6fe27a2d5a212104587427f70c59ba87a (diff)
downloadgleam_stdlib-e174d4eea80dcf28f72c30d78cc17493998687aa.tar.gz
gleam_stdlib-e174d4eea80dcf28f72c30d78cc17493998687aa.zip
Remove `int.InvalidBase`
Diffstat (limited to 'src')
-rw-r--r--src/gleam/int.gleam39
1 files changed, 15 insertions, 24 deletions
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 4318930..6d48f8f 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -170,14 +170,8 @@ pub fn to_string(x: Int) {
@external(javascript, "../gleam_stdlib.mjs", "to_string")
fn do_to_string(a: Int) -> String
-/// Error value when trying to operate with a base out of the allowed range.
-///
-pub type InvalidBase {
- InvalidBase
-}
-
/// Prints a given int to a string using the base number provided.
-/// Supports only bases 2 to 36, for values outside of which this function returns an `Error(InvalidBase)`.
+/// Supports only bases 2 to 36, for values outside of which this function returns an `Error(Nil)`.
/// For common bases (2, 8, 16, 36), use the `to_baseN` functions.
///
/// ## Examples
@@ -199,18 +193,18 @@ pub type InvalidBase {
///
/// ```gleam
/// to_base_string(48, 1)
-/// // -> Error(InvalidBase)
+/// // -> Error(Nil)
/// ```
///
/// ```gleam
/// to_base_string(48, 37)
-/// // -> Error(InvalidBase)
+/// // -> Error(Nil)
/// ```
///
-pub fn to_base_string(x: Int, base: Int) -> Result(String, InvalidBase) {
+pub fn to_base_string(x: Int, base: Int) -> Result(String, Nil) {
case base >= 2 && base <= 36 {
True -> Ok(do_to_base_string(x, base))
- False -> Error(InvalidBase)
+ False -> Error(Nil)
}
}
@@ -467,7 +461,8 @@ fn do_product(numbers: List(Int), initial: Int) -> Int {
}
}
-/// Splits an integer into its digit representation in the specified base
+/// Splits an integer into its digit representation in the specified base.
+/// Returns an error if the base is less than 2.
///
/// ## Examples
///
@@ -478,12 +473,12 @@ fn do_product(numbers: List(Int), initial: Int) -> Int {
///
/// ```gleam
/// digits(234, 1)
-/// // -> Error(InvalidBase)
+/// // -> Error(Nil)
/// ```
///
-pub fn digits(x: Int, base: Int) -> Result(List(Int), InvalidBase) {
+pub fn digits(x: Int, base: Int) -> Result(List(Int), Nil) {
case base < 2 {
- True -> Error(InvalidBase)
+ True -> Error(Nil)
False -> Ok(do_digits(x, base, []))
}
}
@@ -512,24 +507,20 @@ fn do_digits(x: Int, base: Int, acc: List(Int)) -> List(Int) {
///
/// ```gleam
/// undigits([2,3,4], 2)
-/// // -> Error(InvalidBase)
+/// // -> Error(Nil)
/// ```
///
-pub fn undigits(numbers: List(Int), base: Int) -> Result(Int, InvalidBase) {
+pub fn undigits(numbers: List(Int), base: Int) -> Result(Int, Nil) {
case base < 2 {
- True -> Error(InvalidBase)
+ True -> Error(Nil)
False -> do_undigits(numbers, base, 0)
}
}
-fn do_undigits(
- numbers: List(Int),
- base: Int,
- acc: Int,
-) -> Result(Int, InvalidBase) {
+fn do_undigits(numbers: List(Int), base: Int, acc: Int) -> Result(Int, Nil) {
case numbers {
[] -> Ok(acc)
- [digit, ..] if digit >= base -> Error(InvalidBase)
+ [digit, ..] if digit >= base -> Error(Nil)
[digit, ..rest] -> do_undigits(rest, base, acc * base + digit)
}
}