Skip to content

Commit 24efbdd

Browse files
NthPortallrytz
authored andcommitted
bug#10855 Fix IterableOnceOps.min and .max
Use `Ordering.min` and `.max` to implement `IterableOnceOps.min` and `.max`, instead of using `Ordering.lteq` and `.gteq`.
1 parent 26ccfa8 commit 24efbdd

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/library/scala/collection/IterableOnce.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A] =>
724724
def min[B >: A](implicit ord: Ordering[B]): A = {
725725
if (isEmpty)
726726
throw new UnsupportedOperationException("empty.min")
727-
reduceLeft((x, y) => if (ord.lteq(x, y)) x else y)
727+
reduceLeft(ord.min)
728728
}
729729

730730
/** Finds the smallest element.
@@ -761,7 +761,7 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A] =>
761761
def max[B >: A](implicit ord: Ordering[B]): A = {
762762
if (isEmpty)
763763
throw new UnsupportedOperationException("empty.max")
764-
reduceLeft((x, y) => if (ord.gteq(x, y)) x else y)
764+
reduceLeft(ord.max)
765765
}
766766

767767
/** Finds the largest element.

test/junit/scala/collection/TraversableOnceTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,12 @@ class TraversableOnceTest {
9292
assert(Seq(1).minByOption(identity) == Some(1), "minByOption on a Non Empty Iterable has value")
9393
}
9494

95+
@Test
96+
def testMinMaxCorrectness(): Unit = {
97+
import Ordering.Double.IeeeOrdering
98+
val seq = Seq(5.0, 3.0, Double.NaN, 4.0)
99+
assert(seq.min.isNaN)
100+
assert(seq.max.isNaN)
101+
}
102+
95103
}

0 commit comments

Comments
 (0)