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

conversion PartialOrder to PartialOrdering and Hash to Hashing #2116

Merged
merged 5 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ def mimaSettings(moduleName: String) = Seq(
exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"),
exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.MapInstances.catsKernelStdCommutativeMonoidForMap"),
exclude[IncompatibleResultTypeProblem]("cats.data.KleisliInstances0.catsDataMonadForKleisliId")
exclude[IncompatibleResultTypeProblem]("cats.data.KleisliInstances0.catsDataMonadForKleisliId"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.PartialOrderToPartialOrderingConversion.catsKernelPartialOrderingForPartialOrder"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.HashToHashingConversion.catsKernelHashToHashing"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.PartialOrderToPartialOrderingConversion.catsKernelPartialOrderingForPartialOrder"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.HashToHashingConversion.catsKernelHashToHashing")
)
}
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/hash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package instances


trait HashInstances {
trait HashInstances extends kernel.HashToHashingConversion {

implicit val catsContravariantForHash: Contravariant[Hash] =
new Contravariant[Hash] {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/partialOrdering.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package instances

trait PartialOrderingInstances {
trait PartialOrderingInstances extends kernel.PartialOrderToPartialOrderingConversion {
implicit val catsContravariantSemigroupalForPartialOrdering: ContravariantSemigroupal[PartialOrdering] =
new ContravariantSemigroupal[PartialOrdering] {
/** Derive a `PartialOrdering` for `B` given a `PartialOrdering[A]` and a function `B => A`.
Expand Down
15 changes: 15 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 @@ -243,6 +243,21 @@ class Tests extends FunSuite with Discipline {

checkAll("subsetPartialOrder[Int]", PartialOrderTests(subsetPartialOrder[Int]).partialOrder)

{
implicit def subsetPartialOrdering[A]: PartialOrdering[Set[A]] = new PartialOrdering[Set[A]] {

override def tryCompare(x: Set[A], y: Set[A]): Option[Int] = {
if (x == y) Some(0)
else if (x subsetOf y) Some(-1)
else if (y subsetOf x) Some(1)
else None
}

override def lteq(x: Set[A], y: Set[A]): Boolean = (x subsetOf y) || (x == y)
}
checkAll("fromPartialOrdering[Int]", PartialOrderTests(PartialOrder.fromPartialOrdering[Set[Int]]).partialOrder)
}

implicit val arbitraryComparison: Arbitrary[Comparison] =
Arbitrary(Gen.oneOf(Comparison.GreaterThan, Comparison.EqualTo, Comparison.LessThan))

Expand Down
6 changes: 6 additions & 0 deletions kernel/src/main/scala/cats/kernel/Hash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ object Hash extends HashFunctions[Hash] {
}

}

trait HashToHashingConversion {
implicit def catsKernelHashToHashing[A](implicit ev: Hash[A]): Hashing[A] = new Hashing[A] {
override def hash(x: A): Int = ev.hash(x)
}
}
10 changes: 2 additions & 8 deletions kernel/src/main/scala/cats/kernel/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ trait OrderToOrderingConversion {
*/
implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] =
ev.toOrdering

}

object Order extends OrderFunctions[Order] {
object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
/**
* Access an implicit `Order[A]`.
*/
Expand Down Expand Up @@ -161,13 +162,6 @@ object Order extends OrderFunctions[Order] {
def compare(x: A, y: A) = f(x, y)
}

/**
* Implicitly convert a `Order[A]` to a `scala.math.Ordering[A]`
* instance.
*/
implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] =
ev.toOrdering

/**
* An `Order` instance that considers all `A` instances to be equal.
*/
Expand Down
13 changes: 8 additions & 5 deletions kernel/src/main/scala/cats/kernel/PartialOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ abstract class PartialOrderFunctions[P[T] <: PartialOrder[T]] extends EqFunction
ev.gt(x, y)
}

object PartialOrder extends PartialOrderFunctions[PartialOrder] {
object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrderToPartialOrderingConversion {
/**
* Access an implicit `PartialOrder[A]`.
*/
Expand Down Expand Up @@ -154,10 +154,13 @@ object PartialOrder extends PartialOrderFunctions[PartialOrder] {
def partialCompare(x: A, y: A) = f(x, y)
}

/**
* Implicitly convert a `PartialOrder[A]` to a
* `scala.math.PartialOrdering[A]` instance.
*/
def fromPartialOrdering[A](implicit ev: PartialOrdering[A]): PartialOrder[A] = from[A] { (a, b) =>
ev.tryCompare(a, b).fold(Double.NaN)(_.toDouble)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer extending the PartialOrder trait here instead of having so many lambdas (from and the fold).



trait PartialOrderToPartialOrderingConversion {
implicit def catsKernelPartialOrderingForPartialOrder[A](implicit ev: PartialOrder[A]): PartialOrdering[A] =
new PartialOrdering[A] {
def tryCompare(x: A, y: A): Option[Int] = ev.tryCompare(x, y)
Expand Down