Skip to content

Commit

Permalink
Made cats -> scala stdlib conversions homogenous across Eq/PartialOrd…
Browse files Browse the repository at this point in the history
…er/Order/Hash.
  • Loading branch information
denisrosset committed Dec 18, 2017
1 parent 2f836a7 commit 2b0a67f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
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 cats.kernel.EqToEquivConversion {
implicit val catsContravariantMonoidalForEq: ContravariantMonoidal[Eq] =
new ContravariantMonoidal[Eq] {
/**
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 cats.kernel.PartialOrderToPartialOrderingConversion {
implicit val catsContravariantSemigroupalForPartialOrder: ContravariantSemigroupal[PartialOrder] =
new ContravariantSemigroupal[PartialOrder] {
/** Derive a `PartialOrder` for `B` given a `PartialOrder[A]` and a function `B => A`.
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 extends kernel.PartialOrderToPartialOrderingConversion {
trait PartialOrderingInstances {
implicit val catsContravariantSemigroupalForPartialOrdering: ContravariantSemigroupal[PartialOrdering] =
new ContravariantSemigroupal[PartialOrdering] {
/** Derive a `PartialOrdering` for `B` given a `PartialOrdering[A]` and a function `B => A`.
Expand Down
21 changes: 13 additions & 8 deletions kernel/src/main/scala/cats/kernel/Eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ trait Eq[@sp A] extends Any with Serializable { self =>
* Returns `false` if `x` and `y` are equivalent, `true` otherwise.
*/
def neqv(x: A, y: A): Boolean = !eqv(x, y)

/** Returns a `scala.math.Equiv` instance. */
def toEquiv: Equiv[A] = new Equiv[A] {
def equiv(a: A, b: A) = eqv(a, b)
}
}

abstract class EqFunctions[E[T] <: Eq[T]] {
Expand All @@ -32,8 +37,15 @@ 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] = ev.toEquiv
}

object Eq extends EqFunctions[Eq] with EqToEquivConversion {
/**
* Access an implicit `Eq[A]`.
*/
Expand Down Expand Up @@ -66,13 +78,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
17 changes: 11 additions & 6 deletions kernel/src/main/scala/cats/kernel/Hash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ trait Hash[@sp A] extends Any with Eq[A] with Serializable { self =>
*/
def hash(x: A): Int

// `Hash#toHashing` deliberately not implemented since `scala.util.hashing.Hashing` is only
// compatible with universal equality.
/** Returns a `scala.util.hashing.Hashing` instance. */
def toHashing: Hashing[A] = new Hashing[A] {
override def hash(x: A): Int = self.hash(x)
}
}

abstract class HashFunctions[H[T] <: Hash[T]] extends EqFunctions[H] {
Expand All @@ -27,7 +29,7 @@ abstract class HashFunctions[H[T] <: Hash[T]] extends EqFunctions[H] {
}


object Hash extends HashFunctions[Hash] {
object Hash extends HashFunctions[Hash] extends HashToHashingConversion {

/** Fetch a `Hash` instance given the specific type. */
@inline final def apply[A](implicit ev: Hash[A]): Hash[A] = ev
Expand Down Expand Up @@ -56,7 +58,10 @@ 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)
}
/**
* Implicitly derive a `scala.util.hashing.Hashing[A]` from a `Hash[A]`
* instance.
*/
implicit def catsKernelHashToHashing[A](implicit ev: Hash[A]): Hashing[A] =
ev.toHashing
}
1 change: 0 additions & 1 deletion kernel/src/main/scala/cats/kernel/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ trait OrderToOrderingConversion {
*/
implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] =
ev.toOrdering

}

object Order extends OrderFunctions[Order] with OrderToOrderingConversion {
Expand Down
15 changes: 11 additions & 4 deletions kernel/src/main/scala/cats/kernel/PartialOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ trait PartialOrder[@sp A] extends Any with Eq[A] { self =>
* Returns true if `x` > `y`, false otherwise.
*/
def gt(x: A, y: A): Boolean = partialCompare(x, y) > 0

/** Returns a `scala.math.PartialOrdering` instance. */
def toPartialOrdering: PartialOrdering[A] = new PartialOrdering[A] {
def tryCompare(x: A, y: A): Option[Int] = tryCompare(x, y)
def lteq(x: A, y: A): Boolean = lteqv(x, y)
}
}

abstract class PartialOrderFunctions[P[T] <: PartialOrder[T]] extends EqFunctions[P] {
Expand Down Expand Up @@ -162,9 +168,10 @@ object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrde


trait PartialOrderToPartialOrderingConversion {
/**
* Implicitly derive a `scala.math.PartialOrdering[A]` from a `PartialOrder[A]`
* instance.
*/
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)
def lteq(x: A, y: A): Boolean = ev.lteqv(x, y)
}
ev.toPartialOrdering
}

0 comments on commit 2b0a67f

Please sign in to comment.