Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override reverse on reversed PartialOrder to return original instance #1024

Merged
merged 1 commit into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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