aboutsummaryrefslogtreecommitdiff
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
parent94b80cd6fe27a2d5a212104587427f70c59ba87a (diff)
downloadgleam_stdlib-e174d4eea80dcf28f72c30d78cc17493998687aa.tar.gz
gleam_stdlib-e174d4eea80dcf28f72c30d78cc17493998687aa.zip
Remove `int.InvalidBase`
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam/int.gleam39
-rw-r--r--test/gleam/int_test.gleam10
3 files changed, 21 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 252df96..94153e7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@
- Fixed a bug where `string.trim` could remove commas on JavaScript.
- The `string.pop_grapheme` function has been optimised on Erlang, greatly
improving its performance.
+- The `InvalidBase` error in the `int` module has been replaced by `Nil`.
## v0.39.0 - 2024-07-09
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)
}
}
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index e3ee369..f9b92dd 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -102,11 +102,11 @@ pub fn to_base_string_test() {
100
|> int.to_base_string(1)
- |> should.equal(Error(int.InvalidBase))
+ |> should.equal(Error(Nil))
100
|> int.to_base_string(37)
- |> should.equal(Error(int.InvalidBase))
+ |> should.equal(Error(Nil))
}
pub fn to_base2_test() {
@@ -369,7 +369,7 @@ pub fn digits_test() {
|> should.equal(Ok([1, 1, 1, 1, 0, 1, 1]))
int.digits(123, 1)
- |> should.equal(Error(int.InvalidBase))
+ |> should.equal(Error(Nil))
}
pub fn undigits_test() {
@@ -386,10 +386,10 @@ pub fn undigits_test() {
|> should.equal(Ok(123))
int.undigits([1, 2, 3], 1)
- |> should.equal(Error(int.InvalidBase))
+ |> should.equal(Error(Nil))
int.undigits([1, 1, 2], 2)
- |> should.equal(Error(int.InvalidBase))
+ |> should.equal(Error(Nil))
}
pub fn random_test() {