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 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
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.EqToEquivConversion.catsKernelEquivForEq"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.OrderToOrderingConversion.catsKernelOrderingForOrder"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.HashToHashingConversion.catsKernelHashToHashing")
)
}
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/eq.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package instances

trait EqInstances {
trait EqInstances extends kernel.instances.EqInstances {
implicit val catsContravariantMonoidalForEq: ContravariantMonoidal[Eq] =
new ContravariantMonoidal[Eq] {
/**
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.instances.HashInstances {

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

trait OrderInstances extends cats.kernel.OrderToOrderingConversion {
trait OrderInstances extends kernel.instances.OrderInstances {
implicit val catsContravariantMonoidalForOrder: ContravariantMonoidal[Order] =
new ContravariantMonoidal[Order] {
/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/partialOrder.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package instances

trait PartialOrderInstances {
trait PartialOrderInstances extends kernel.instances.PartialOrderInstances {
implicit val catsContravariantSemigroupalForPartialOrder: ContravariantSemigroupal[PartialOrder] =
new ContravariantSemigroupal[PartialOrder] {
/** Derive a `PartialOrder` for `B` given a `PartialOrder[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
19 changes: 11 additions & 8 deletions kernel/src/main/scala/cats/kernel/Eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ abstract class EqFunctions[E[T] <: Eq[T]] {

}

object Eq extends EqFunctions[Eq] {
trait EqToEquivConversion {
/**
* Implicitly derive a `scala.math.Equiv[A]` from a `Eq[A]`
* instance.
*/
implicit def catsKernelEquivForEq[A](implicit ev: Eq[A]): Equiv[A] = new Equiv[A] {
def equiv(a: A, b: A) = ev.eqv(a, b)
}
}

object Eq extends EqFunctions[Eq] with EqToEquivConversion {

/**
* Access an implicit `Eq[A]`.
Expand Down Expand Up @@ -66,13 +76,6 @@ object Eq extends EqFunctions[Eq] {
def eqv(x: A, y: A) = eq1.eqv(x, y) || eq2.eqv(x, y)
}

/**
* This gives compatibility with scala's Equiv trait
*/
implicit def catsKernelEquivForEq[A](implicit ev: Eq[A]): Equiv[A] =
new Equiv[A] {
def equiv(a: A, b: A) = ev.eqv(a, b)
}

/**
* Create an `Eq` instance from an `eqv` implementation.
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
14 changes: 9 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,14 @@ 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] = new PartialOrder[A] {
def partialCompare(x: A, y: A): Double =
ev.tryCompare(x, y).fold(Double.NaN)(_.toDouble)
}
}


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
4 changes: 4 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ trait AllInstances
with ByteInstances
with CharInstances
with DoubleInstances
with EqInstances
with EitherInstances
with DurationInstances
with FloatInstances
with FunctionInstances
with HashInstances
with IntInstances
with ListInstances
with LongInstances
with MapInstances
with OptionInstances
with OrderInstances
with PartialOrderInstances
with QueueInstances
with SetInstances
with ShortInstances
Expand Down
6 changes: 6 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/eq.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cats.kernel
package instances

trait EqInstances extends EqToEquivConversion

object eq extends EqInstances
6 changes: 6 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/hash.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cats.kernel
package instances

trait HashInstances extends HashToHashingConversion

object hash extends HashInstances
6 changes: 6 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/order.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cats.kernel
package instances

trait OrderInstances extends OrderToOrderingConversion

object order extends OrderInstances
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cats.kernel
package instances

trait PartialOrderInstances extends PartialOrderToPartialOrderingConversion

object partialOrder extends PartialOrderInstances