Skip to content

Commit

Permalink
Add std::cmp.min and std::cmp.max
Browse files Browse the repository at this point in the history
These methods take two values and return the minimum and maximum value
respectively.

Changelog: added
  • Loading branch information
yorickpeterse committed Oct 13, 2022
1 parent 1a320f5 commit c335285
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
22 changes: 22 additions & 0 deletions libstd/src/std/cmp.inko
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,25 @@ trait pub Contains[T] {
# Returns `true` if the given value is contained in `self`.
fn pub contains?(value: ref T) -> Bool
}

# Returns the minimum of two values.
#
# # Examples
#
# import std::cmp::(min)
#
# min(10, 5) # => 5
fn pub min[T: Compare](a: T, b: T) -> T {
if a <= b { a } else { b }
}

# Returns the maximum of two values.
#
# # Examples
#
# import std::cmp::(max)
#
# max(10, 5) # => 10
fn pub max[T: Compare](a: T, b: T) -> T {
if a >= b { a } else { b }
}
14 changes: 13 additions & 1 deletion libstd/test/std/test_cmp.inko
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import helpers::(fmt)
import std::cmp::(Compare, Equal, Ordering)
import std::cmp::(Compare, Equal, Ordering, max, min)
import std::test::Tests

class enum Letter {
Expand Down Expand Up @@ -82,4 +82,16 @@ fn pub tests(t: mut Tests) {
t.true(Letter.A == Letter.A)
t.true(Letter.A != Letter.B)
}

t.test('cmp.min') fn (t) {
t.equal(min(10, 10), 10)
t.equal(min(10, 5), 5)
t.equal(min(5, 10), 5)
}

t.test('cmp.max') fn (t) {
t.equal(max(10, 10), 10)
t.equal(max(10, 5), 10)
t.equal(max(5, 10), 10)
}
}

0 comments on commit c335285

Please sign in to comment.