aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsobolevn <mail@sobolevn.me>2024-08-20 10:31:20 +0300
committerLouis Pilfold <louis@lpil.uk>2024-08-24 13:07:53 +0100
commite57ea3d5b73f9143071941ec0da0b6c38de7d86a (patch)
tree913246f398b404d8f48efddef866454d19aad20f
parent7f348a800e676f753756712673aabc5dde66594d (diff)
downloadgleam_stdlib-e57ea3d5b73f9143071941ec0da0b6c38de7d86a.tar.gz
gleam_stdlib-e57ea3d5b73f9143071941ec0da0b6c38de7d86a.zip
Address review
-rw-r--r--src/gleam/bit_array.gleam23
-rw-r--r--test/gleam/bit_array_test.gleam2
2 files changed, 11 insertions, 14 deletions
diff --git a/src/gleam/bit_array.gleam b/src/gleam/bit_array.gleam
index 8c3ce48..cba8da9 100644
--- a/src/gleam/bit_array.gleam
+++ b/src/gleam/bit_array.gleam
@@ -200,26 +200,24 @@ fn do_inspect(input: BitArray, accumulator: String) -> String {
/// compare(<<"AB":utf8>>, <<"AA":utf8>>)
/// // -> Gt
///
-/// compare(<<1, 2:size(2)>>, <<1, 2:size(2)>>)
+/// compare(<<1, 2:size(2)>>, with: <<1, 2:size(2)>>)
/// // -> Eq
/// ```
///
/// Only supported on Erlang target for now.
///
-pub fn compare(first: BitArray, second: BitArray) -> order.Order {
- do_compare(first, second)
+pub fn compare(a: BitArray, with b: BitArray) -> order.Order {
+ do_compare(a, b)
}
-@target(javascript)
@external(javascript, "../gleam_stdlib.mjs", "bit_array_compare")
-fn do_compare(first: BitArray, second: BitArray) -> order.Order
-
-@target(erlang)
-fn do_compare(first: BitArray, second: BitArray) -> order.Order {
- case first, second {
+fn do_compare(a: BitArray, with b: BitArray) -> order.Order {
+ case a, b {
<<first_byte, first_rest:bits>>, <<second_byte, second_rest:bits>> ->
- int.compare(first_byte, second_byte)
- |> order.lazy_break_tie(fn() { compare(first_rest, second_rest) })
+ case int.compare(first_byte, second_byte) {
+ order.Eq -> compare(first_rest, second_rest)
+ result -> result
+ }
<<>>, <<>> -> order.Eq
// First has more items, example: "AB" > "A":
@@ -233,6 +231,5 @@ fn do_compare(first: BitArray, second: BitArray) -> order.Order {
}
}
-@target(erlang)
@external(erlang, "gleam_stdlib", "bit_array_to_int")
-fn bit_array_to_int(first: BitArray) -> Int
+fn bit_array_to_int(a: BitArray) -> Int
diff --git a/test/gleam/bit_array_test.gleam b/test/gleam/bit_array_test.gleam
index 1fa38c1..553a8db 100644
--- a/test/gleam/bit_array_test.gleam
+++ b/test/gleam/bit_array_test.gleam
@@ -338,7 +338,7 @@ pub fn compare_test() {
bit_array.compare(<<1, 2, 3>>, <<1, 2, 3>>)
|> should.equal(order.Eq)
- bit_array.compare(<<1, 2>>, <<1, 3>>)
+ bit_array.compare(<<1, 2>>, with: <<1, 3>>)
|> should.equal(order.Lt)
bit_array.compare(<<1, 3>>, <<1, 2>>)