Skip to content

Commit

Permalink
Merge pull request #1005 from hylo-lang/min-max-int
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga authored Sep 13, 2023
2 parents 2536c56 + af558bc commit b99e9e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Library/Hylo/Core/Comparable.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ extension Comparable {
fun infix>= (_ other: Self) -> Bool { !(self < other) }

}

/// Returns the lesser of `x` and `y`.
public subscript min<T: Comparable>(_ x: T, _ y: T): T {
if y < x { yield y } else { yield x }
}

/// Returns the greater of `x` and `y`.
public subscript max<T: Comparable>(_ x: T, _ y: T): T {
if x < y { yield y } else { yield x }
}
2 changes: 2 additions & 0 deletions Library/Hylo/Core/Int.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public conformance Int: Copyable {

}

public conformance Int: Comparable {}

public conformance Int: ForeignConvertible {

public typealias ForeignRepresentation = Builtin.word
Expand Down
15 changes: 15 additions & 0 deletions Tests/LibraryTests/TestCases/IntTests.hylo
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
//- compileAndRun expecting: success

fun test_min() {
precondition(min[1, 2] == 1)
precondition(min[1, 1] == 1)
precondition(min[2, 1] == 1)
}

fun test_max() {
precondition(max[1, 2] == 2)
precondition(max[1, 1] == 1)
precondition(max[2, 1] == 2)
}

public fun main() {
// Int.init()
precondition(Int() == 0)

test_min()
test_max()
}

0 comments on commit b99e9e9

Please sign in to comment.