Skip to content

Commit

Permalink
Add {C,c}omparison to Order, fixed typelevel#1101
Browse files Browse the repository at this point in the history
  • Loading branch information
adelbertc committed Jun 9, 2016
1 parent 88cbe95 commit 775ed65
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 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 @@ -79,6 +79,13 @@ class LawTests extends FunSuite with Discipline {
laws[GroupLaws, (Int, Int)].check(_.band)

laws[GroupLaws, Unit].check(_.boundedSemilattice)

test("signum . toInt . comparison = signum . compare") {
check { (i: Int, j: Int) =>
Eq[Int].eqv(Order[Int].comparison(i, j).toInt.signum, Order[Int].compare(i, j).signum)
}
}

// esoteric machinery follows...

implicit lazy val band: Band[(Int, Int)] =
Expand Down
17 changes: 17 additions & 0 deletions kernel/src/main/scala/cats/kernel/Comparison.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cats.kernel

/** ADT encoding the possible results of a comparison */
sealed abstract class Comparison extends Product with Serializable {
/** The signum of this comparison */
def toInt: Int = this match {
case Comparison.GreaterThan => 1
case Comparison.EqualTo => 0
case Comparison.LessThan => -1
}
}

object Comparison {
final case object GreaterThan extends Comparison
final case object EqualTo extends Comparison
final case object LessThan extends Comparison
}
11 changes: 11 additions & 0 deletions kernel/src/main/scala/cats/kernel/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ trait Order[@sp A] extends Any with PartialOrder[A] { self =>
*/
def compare(x: A, y: A): Int

/**
* Like `compare`, but returns a [[cats.kernel.Comparison]] instead of an Int.
* Has the benefit of being able to pattern match on, but not as performant.
*/
def comparison(x: A, y: A): Comparison = {
val r = compare(x, y)
if (r > 0) Comparison.GreaterThan
else if (r == 0) Comparison.EqualTo
else Comparison.LessThan
}

def partialCompare(x: A, y: A): Double = compare(x, y).toDouble

/**
Expand Down

0 comments on commit 775ed65

Please sign in to comment.