diff options
author | Brett Snyder <bsnyder@digitalocean.com> | 2019-10-03 08:42:01 -0500 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-10-03 14:42:01 +0100 |
commit | cc7ba56105a6f8933c2bfd25151061a94471eb23 (patch) | |
tree | bcf8a3977cb0a37bffe7be4f527436d94f7afeb1 | |
parent | cf7831f278423bab72f5d9433771e5a16ce44cac (diff) | |
download | gleam_stdlib-cc7ba56105a6f8933c2bfd25151061a94471eb23.tar.gz gleam_stdlib-cc7ba56105a6f8933c2bfd25151061a94471eb23.zip |
add min() for Int and Float modules (#268)
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | gen/src/gleam@float.erl | 11 | ||||
-rw-r--r-- | gen/src/gleam@int.erl | 11 | ||||
-rw-r--r-- | gen/test/gleam@float_test.erl | 11 | ||||
-rw-r--r-- | gen/test/gleam@int_test.erl | 10 | ||||
-rw-r--r-- | src/gleam/float.gleam | 7 | ||||
-rw-r--r-- | src/gleam/int.gleam | 8 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 23 | ||||
-rw-r--r-- | test/gleam/int_test.gleam | 20 |
9 files changed, 99 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d31575e..8c8370c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - The `pair` module gains the `map_first`, and `map_second` functions. - The `string` module gains the `compare` function. +- The `float` module gains the `min` function. +- The `int` module gains the `min` function. ## v0.4.0 - 2019-09-19 diff --git a/gen/src/gleam@float.erl b/gen/src/gleam@float.erl index 6aab56b..625ba56 100644 --- a/gen/src/gleam@float.erl +++ b/gen/src/gleam@float.erl @@ -1,7 +1,7 @@ -module(gleam@float). -compile(no_auto_import). --export([parse/1, to_string/1, compare/2, ceiling/1, floor/1, round/1, truncate/1]). +-export([parse/1, to_string/1, compare/2, min/2, ceiling/1, floor/1, round/1, truncate/1]). parse(A) -> gleam_stdlib:parse_float(A). @@ -24,6 +24,15 @@ compare(A, B) -> end end. +min(A, B) -> + case A < B of + true -> + A; + + false -> + B + end. + ceiling(A) -> math:ceil(A). diff --git a/gen/src/gleam@int.erl b/gen/src/gleam@int.erl index ea97785..de9ce8b 100644 --- a/gen/src/gleam@int.erl +++ b/gen/src/gleam@int.erl @@ -1,7 +1,7 @@ -module(gleam@int). -compile(no_auto_import). --export([parse/1, to_string/1, to_base_string/2, compare/2]). +-export([parse/1, to_string/1, to_base_string/2, compare/2, min/2]). parse(A) -> gleam_stdlib:parse_int(A). @@ -26,3 +26,12 @@ compare(A, B) -> gt end end. + +min(A, B) -> + case A < B of + true -> + A; + + false -> + B + end. diff --git a/gen/test/gleam@float_test.erl b/gen/test/gleam@float_test.erl index 0f7e55b..d60997e 100644 --- a/gen/test/gleam@float_test.erl +++ b/gen/test/gleam@float_test.erl @@ -1,7 +1,7 @@ -module(gleam@float_test). -compile(no_auto_import). --export([parse_test/0, to_string_test/0, compare_test/0, ceiling_test/0, floor_test/0, round_test/0, truncate_test/0]). +-export([parse_test/0, to_string_test/0, compare_test/0, ceiling_test/0, floor_test/0, round_test/0, truncate_test/0, min_test/0]). parse_test() -> gleam@expect:equal(gleam@float:parse(<<"1.23">>), {ok, 1.23}), @@ -48,3 +48,12 @@ truncate_test() -> gleam@expect:equal(gleam@float:truncate(8.5), 8), gleam@expect:equal(gleam@float:truncate(-8.1), -8), gleam@expect:equal(gleam@float:truncate(-7.5), -7). + +min_test() -> + gleam@expect:equal(gleam@float:min(0.0, 0.0), 0.0), + gleam@expect:equal(gleam@float:min(0.3, 1.5), 0.3), + gleam@expect:equal(gleam@float:min(1.0, 0.0), 0.0), + gleam@expect:equal(gleam@float:min(-1.7, 2.5), -1.7), + gleam@expect:equal(gleam@float:min(-2.2, -2.2), -2.2), + gleam@expect:equal(gleam@float:min(-1.0, -1.0), -1.0), + gleam@expect:equal(gleam@float:min(-1.1, -1.0), -1.1). diff --git a/gen/test/gleam@int_test.erl b/gen/test/gleam@int_test.erl index 7cee67a..ce8936c 100644 --- a/gen/test/gleam@int_test.erl +++ b/gen/test/gleam@int_test.erl @@ -1,7 +1,7 @@ -module(gleam@int_test). -compile(no_auto_import). --export([to_string/0, parse/0, to_base_string/0, compare_test/0]). +-export([to_string/0, parse/0, to_base_string/0, compare_test/0, min_test/0]). to_string() -> gleam@expect:equal(gleam@int:to_string(123), <<"123">>), @@ -27,3 +27,11 @@ compare_test() -> gleam@expect:equal(gleam@int:compare(-2, -1), lt), gleam@expect:equal(gleam@int:compare(2, 1), gt), gleam@expect:equal(gleam@int:compare(-1, -2), gt). + +min_test() -> + gleam@expect:equal(gleam@int:min(0, 0), 0), + gleam@expect:equal(gleam@int:min(0, 1), 0), + gleam@expect:equal(gleam@int:min(1, 0), 0), + gleam@expect:equal(gleam@int:min(-1, 2), -1), + gleam@expect:equal(gleam@int:min(2, -2), -2), + gleam@expect:equal(gleam@int:min(-1, -1), -1). diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 441d9a0..3929218 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -21,6 +21,13 @@ pub fn compare(a, b) { } } +pub fn min(a, b) { + case a <. b { + | True -> a + | False -> b + } +} + pub external fn ceiling(Float) -> Float = "math" "ceil"; pub external fn floor(Float) -> Float = "math" "floor"; diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 10731a3..ddd0ac5 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -16,3 +16,11 @@ pub fn compare(a, b) { } } } + +pub fn min(a, b) { + case a < b { + | True -> a + | False -> b + } +} + diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index c416808..88c9c64 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -137,3 +137,26 @@ pub fn truncate_test() { |> float.truncate |> expect.equal(_, -7) } + +pub fn min_test() { + float.min(0., 0.) + |> expect.equal(_, 0.) + + float.min(0.3, 1.5) + |> expect.equal(_, 0.3) + + float.min(1., 0.) + |> expect.equal(_, 0.) + + float.min(-1.7, 2.5) + |> expect.equal(_, -1.7) + + float.min(-2.2, -2.2) + |> expect.equal(_, -2.2) + + float.min(-1., -1.) + |> expect.equal(_, -1.) + + float.min(-1.1, -1.) + |> expect.equal(_, -1.1) +} diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index dcfece7..5ef20a7 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -71,3 +71,23 @@ pub fn compare_test() { int.compare(-1, -2) |> expect.equal(_, order.Gt) } + +pub fn min_test() { + int.min(0, 0) + |> expect.equal(_, 0) + + int.min(0, 1) + |> expect.equal(_, 0) + + int.min(1, 0) + |> expect.equal(_, 0) + + int.min(-1, 2) + |> expect.equal(_, -1) + + int.min(2, -2) + |> expect.equal(_, -2) + + int.min(-1, -1) + |> expect.equal(_, -1) +} |