Skip to content

Commit

Permalink
Merge pull request #1024 from ceedubs/partial-order-reverse
Browse files Browse the repository at this point in the history
Override reverse on reversed PartialOrder to return original instance
  • Loading branch information
fthomas committed May 10, 2016
2 parents db1fc3e + caaf3f9 commit c7498e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class LawTests extends FunSuite with Discipline {
laws[OrderLaws, Stream[HasEq[Int]]].check(_.eqv)

laws[OrderLaws, Set[Int]].check(_.partialOrder)
laws[OrderLaws, Set[Int]]("reverse").check(_.partialOrder(PartialOrder[Set[Int]].reverse))
laws[OrderLaws, Set[Int]]("reverse.reverse").check(_.partialOrder(PartialOrder[Set[Int]].reverse.reverse))
laws[OrderLaws, Option[HasPartialOrder[Int]]].check(_.partialOrder)
laws[OrderLaws, List[HasPartialOrder[Int]]].check(_.partialOrder)
laws[OrderLaws, Vector[HasPartialOrder[Int]]].check(_.partialOrder)
Expand All @@ -53,6 +55,8 @@ class LawTests extends FunSuite with Discipline {
laws[OrderLaws, Vector[Int]].check(_.order)
laws[OrderLaws, Stream[Int]].check(_.order)
laws[OrderLaws, Int]("fromOrdering").check(_.order(Order.fromOrdering[Int]))
laws[OrderLaws, Int]("reverse").check(_.order(Order[Int].reverse))
laws[OrderLaws, Int]("reverse.reverse").check(_.order(Order[Int].reverse.reverse))

laws[GroupLaws, String].check(_.monoid)
laws[GroupLaws, Option[Int]].check(_.monoid)
Expand Down
12 changes: 7 additions & 5 deletions kernel/src/main/scala/cats/kernel/PartialOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import scala.{specialized => sp}

/**
* The `PartialOrder` type class is used to define a partial ordering on some type `A`.
*
*
* A partial order is defined by a relation <=, which obeys the following laws:
*
*
* - x <= x (reflexivity)
* - if x <= y and y <= x, then x = y (anti-symmetry)
* - if x <= y and y <= z, then x <= z (transitivity)
*
*
* To compute both <= and >= at the same time, we use a Double number
* to encode the result of the comparisons x <= y and x >= y.
* The truth table is defined as follows:
*
*
* x <= y x >= y Double
* true true = 0.0 (corresponds to x = y)
* false false = NaN (x and y cannot be compared)
Expand All @@ -41,7 +41,7 @@ trait PartialOrder[@sp A] extends Any with Eq[A] { self =>
* - negative iff `x < y`
* - zero iff `x = y`
* - positive iff `x > y`
*/
*/
def tryCompare(x: A, y: A): Option[Int] = {
val c = partialCompare(x, y)
if (isNaN(c)) None else Some(c.signum)
Expand Down Expand Up @@ -82,6 +82,8 @@ trait PartialOrder[@sp A] extends Any with Eq[A] { self =>
def reverse: PartialOrder[A] =
new PartialOrder[A] {
def partialCompare(x: A, y: A): Double = self.partialCompare(y, x)

override def reverse: PartialOrder[A] = self
}

// The following may be overridden for performance:
Expand Down

0 comments on commit c7498e4

Please sign in to comment.