Skip to content

Commit

Permalink
fix #1398
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jun 15, 2017
1 parent f6f4d25 commit 86bce70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
17 changes: 14 additions & 3 deletions core/src/main/scala/scalaz/Foldable.scala
Expand Up @@ -253,9 +253,20 @@ trait Foldable[F[_]] { self =>
} {
case (x @ ((amin, amax, bmin, bmax)), a) =>
val b = f(a)
if (Order[B].order(b, bmin) == LT) (a, amax, b, bmax)
else if (Order[B].order(b, bmax) == GT) (amin, a, bmin, b)
else x
val greaterThanOrEq = Order[B].greaterThanOrEqual(b, bmax)
if(Order[B].lessThanOrEqual(b, bmin)) {
if(greaterThanOrEq) {
(a, a, b, b)
} else {
(a, amax, b, bmax)
}
} else {
if(greaterThanOrEq) {
(amin, a, bmin, b)
} else {
x
}
}
} map {
case (amin, amax, _, _) => (amin, amax)
}
Expand Down
11 changes: 9 additions & 2 deletions tests/src/test/scala/scalaz/FoldableTest.scala
Expand Up @@ -69,10 +69,17 @@ object FoldableTest extends SpecLite {
(xs extremaOf f) must_== (xs minimumOf f).tuple(xs maximumOf f)
}
"extremaBy" ! forAll {
(xs: List[Int]) =>
val f: Int => Double = 1D + _
(xs: List[Int], f: Int => Int) =>
(xs extremaBy f) must_== (xs minimumBy f).tuple(xs maximumBy f)
}
"extremaBy consistent with minimumBy/maximumBy" ! {
val xs = (1 to 6).toList
val f: Int => Int = _ % 3
(xs extremaBy f) must_== (xs minimumBy f).tuple(xs maximumBy f)

val g: Int => Int = _ => 0
(xs extremaBy g) must_== (xs minimumBy g).tuple(xs maximumBy g)
}

"distinct" ! forAll {
(xs: List[Int]) =>
Expand Down

0 comments on commit 86bce70

Please sign in to comment.