diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-05-10 14:11:45 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-05-10 14:11:45 +0100 |
commit | e1f4d26f3ee2e765c87b02c96950c9e0cb1d8bde (patch) | |
tree | 3a0000a1d357a6a47e5b9f25cdd552e9e9bd3d5a | |
parent | 49df300ffe6f020a4df26dede27bef42d000c671 (diff) | |
download | gleam_stdlib-e1f4d26f3ee2e765c87b02c96950c9e0cb1d8bde.tar.gz gleam_stdlib-e1f4d26f3ee2e765c87b02c96950c9e0cb1d8bde.zip |
int:compare
-rw-r--r-- | gen/src/int.erl | 17 | ||||
-rw-r--r-- | gen/test/int_test.erl | 10 | ||||
-rw-r--r-- | src/int.gleam | 13 | ||||
-rw-r--r-- | test/int_test.gleam | 21 |
4 files changed, 59 insertions, 2 deletions
diff --git a/gen/src/int.erl b/gen/src/int.erl index d3b5939..21ad7d3 100644 --- a/gen/src/int.erl +++ b/gen/src/int.erl @@ -1,7 +1,7 @@ -module(int). -compile(no_auto_import). --export([parse/1, to_string/1, to_base_string/2]). +-export([parse/1, to_string/1, to_base_string/2, compare/2]). parse(A) -> gleam__stdlib:parse_int(A). @@ -11,3 +11,18 @@ to_string(A) -> to_base_string(A, B) -> erlang:integer_to_binary(A, B). + +compare(A, B) -> + case A =:= B of + true -> + eq; + + false -> + case A < B of + true -> + lt; + + false -> + gt + end + end. diff --git a/gen/test/int_test.erl b/gen/test/int_test.erl index a50b5e6..0d3163e 100644 --- a/gen/test/int_test.erl +++ b/gen/test/int_test.erl @@ -1,7 +1,7 @@ -module(int_test). -compile(no_auto_import). --export([to_string/0, parse/0, to_base_string/0]). +-export([to_string/0, parse/0, to_base_string/0, compare_test/0]). to_string() -> expect:equal(int:to_string(123), <<"123">>), @@ -19,3 +19,11 @@ parse() -> to_base_string() -> expect:equal(int:to_base_string(100, 16), <<"64">>), expect:equal(int:to_base_string(-100, 16), <<"-64">>). + +compare_test() -> + expect:equal(int:compare(0, 0), eq), + expect:equal(int:compare(1, 1), eq), + expect:equal(int:compare(0, 1), lt), + expect:equal(int:compare(-2, -1), lt), + expect:equal(int:compare(2, 1), gt), + expect:equal(int:compare(-1, -2), gt). diff --git a/src/int.gleam b/src/int.gleam index 00571de..5eeece4 100644 --- a/src/int.gleam +++ b/src/int.gleam @@ -1,3 +1,5 @@ +import order + pub enum NotAnInt = | NotAnInt @@ -6,3 +8,14 @@ pub external fn parse(String) -> Result(Int, NotAnInt) = "gleam__stdlib" "parse_ pub external fn to_string(Int) -> String = "erlang" "integer_to_binary" pub external fn to_base_string(Int, Int) -> String = "erlang" "integer_to_binary" + +pub fn compare(a, b) { + case a == b { + | True -> order:Eq + | False -> + case a < b { + | True -> order:Lt + | False -> order:Gt + } + } +} diff --git a/test/int_test.gleam b/test/int_test.gleam index d5f0f58..77f48ba 100644 --- a/test/int_test.gleam +++ b/test/int_test.gleam @@ -1,5 +1,6 @@ import expect import int +import order pub fn to_string() { 123 @@ -50,3 +51,23 @@ pub fn to_base_string() { |> int:to_base_string(_, 16) |> expect:equal(_, "-64") } + +pub fn compare_test() { + int:compare(0, 0) + |> expect:equal(_, order:Eq) + + int:compare(1, 1) + |> expect:equal(_, order:Eq) + + int:compare(0, 1) + |> expect:equal(_, order:Lt) + + int:compare(-2, -1) + |> expect:equal(_, order:Lt) + + int:compare(2, 1) + |> expect:equal(_, order:Gt) + + int:compare(-1, -2) + |> expect:equal(_, order:Gt) +} |