Skip to content

Commit

Permalink
Removed extra methods on the kernel typeclasses.
Browse files Browse the repository at this point in the history
  • Loading branch information
denisrosset committed Oct 30, 2017
1 parent b966219 commit cedb6ee
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 73 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/IdT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private[data] sealed abstract class IdTInstances0 extends IdTInstances1 {
new IdTTraverse[F] { implicit val F0: Traverse[F] = F }

implicit def catsDataEqForIdT[F[_], A](implicit F: Eq[F[A]]): Eq[IdT[F, A]] =
F.on(_.value)
Eq.by[IdT[F, A], F[A]](_.value)
}

private[data] sealed abstract class IdTInstances extends IdTInstances0 {
Expand All @@ -160,7 +160,7 @@ private[data] sealed abstract class IdTInstances extends IdTInstances0 {
new IdTNonEmptyTraverse[F] { implicit val F0: NonEmptyTraverse[F] = F }

implicit def catsDataOrderForIdT[F[_], A](implicit F: Order[F[A]]): Order[IdT[F, A]] =
F.on(_.value)
Order.by[IdT[F, A], F[A]](_.value)

implicit def catsDataShowForIdT[F[_], A](implicit F: Show[F[A]]): Show[IdT[F, A]] =
Contravariant[Show].contramap(F)(_.value)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/Nested.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Nested extends NestedInstances

private[data] sealed abstract class NestedInstances extends NestedInstances0 {
implicit def catsDataEqForNested[F[_], G[_], A](implicit FGA: Eq[F[G[A]]]): Eq[Nested[F, G, A]] =
FGA.on(_.value)
Eq.by[Nested[F, G, A], F[G[A]]](_.value)

implicit def catsDataNonEmptyTraverseForNested[F[_]: NonEmptyTraverse, G[_]: NonEmptyTraverse]: NonEmptyTraverse[Nested[F, G, ?]] =
new NestedNonEmptyTraverse[F, G] {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2
catsDataMonadForWriterT[Id, L]

implicit def catsDataEqForWriterT[F[_], L, V](implicit F: Eq[F[(L, V)]]): Eq[WriterT[F, L, V]] =
F.on(_.run)
Eq.by[WriterT[F, L, V], F[(L, V)]](_.run)

implicit def catsDataSemigroupForWriterTId[L:Semigroup, V:Semigroup]: Semigroup[WriterT[Id, L, V]] =
catsDataSemigroupForWriterT[Id, L, V]
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/instances/eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package instances
trait EqInstances {
implicit val catsContravariantSemigroupalForEq: ContravariantSemigroupal[Eq] =
new ContravariantSemigroupal[Eq] {
def contramap[A, B](fa: Eq[A])(fn: B => A): Eq[B] =
fa.on(fn)
def contramap[A, B](fa: Eq[A])(fn: B => A): Eq[B] = Eq.by[B, A](fn)(fa)

def product[A, B](fa: Eq[A], fb: Eq[B]): Eq[(A, B)] =
Eq.instance { (left, right) => fa.eqv(left._1, right._1) && fb.eqv(left._2, right._2) }
}
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
Expand Up @@ -9,7 +9,7 @@ trait OrderInstances extends cats.kernel.OrderToOrderingConversion {
*
* Note: resulting instances are law-abiding only when the functions used are injective (represent a one-to-one mapping)
*/
def contramap[A, B](fa: Order[A])(f: B => A): Order[B] = fa.on(f)
def contramap[A, B](fa: Order[A])(f: B => A): Order[B] = Order.by[B, A](f)(fa)

def product[A, B](fa: Order[A], fb: Order[B]): Order[(A, B)] =
new Order[(A, B)] {
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
Expand Up @@ -8,7 +8,7 @@ trait PartialOrderInstances {
*
* Note: resulting instances are law-abiding only when the functions used are injective (represent a one-to-one mapping)
*/
def contramap[A, B](fa: PartialOrder[A])(f: B => A): PartialOrder[B] = fa.on(f)
def contramap[A, B](fa: PartialOrder[A])(f: B => A): PartialOrder[B] = PartialOrder.by[B, A](f)(fa)

def product[A, B](fa: PartialOrder[A], fb: PartialOrder[B]): PartialOrder[(A, B)] =
new PartialOrder[(A, B)] {
Expand Down
8 changes: 4 additions & 4 deletions kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class Tests extends FunSuite with Discipline {
checkAll("Eq[Queue[HasEq[Int]]]", EqTests[Queue[HasEq[Int]]].eqv)

checkAll("PartialOrder[Set[Int]]", PartialOrderTests[Set[Int]].partialOrder)
checkAll("PartialOrder[Set[Int]].reverse", PartialOrderTests(PartialOrder[Set[Int]].reverse).partialOrder)
checkAll("PartialOrder[Set[Int]].reverse.reverse", PartialOrderTests(PartialOrder[Set[Int]].reverse.reverse).partialOrder)
checkAll("PartialOrder.reverse(PartialOrder[Set[Int]])", PartialOrderLawTests(PartialOrder.reverse(PartialOrder[Set[Int]])).partialOrder)
checkAll("PartialOrder.reverse(PartialOrder.reverse(PartialOrder[Set[Int]]))", PartialOrderLawTests(PartialOrder.reverse(PartialOrder.reverse(PartialOrder[Set[Int]]))).partialOrder)
checkAll("PartialOrder[Option[HasPartialOrder[Int]]]", PartialOrderTests[Option[HasPartialOrder[Int]]].partialOrder)
checkAll("PartialOrder[List[HasPartialOrder[Int]]]", PartialOrderTests[List[HasPartialOrder[Int]]].partialOrder)
checkAll("PartialOrder[Vector[HasPartialOrder[Int]]]", PartialOrderTests[Vector[HasPartialOrder[Int]]].partialOrder)
Expand Down Expand Up @@ -343,7 +343,7 @@ class Tests extends FunSuite with Discipline {

object HasEq {
implicit def hasEq[A: Eq]: Eq[HasEq[A]] =
Eq[A].on(_.a)
Eq.by(_.a)
implicit def hasEqArbitrary[A: Arbitrary]: Arbitrary[HasEq[A]] =
Arbitrary(arbitrary[A].map(HasEq(_)))
implicit def hasCogen[A: Cogen]: Cogen[HasEq[A]] =
Expand All @@ -354,7 +354,7 @@ class Tests extends FunSuite with Discipline {

object HasPartialOrder {
implicit def hasPartialOrder[A: PartialOrder]: PartialOrder[HasPartialOrder[A]] =
PartialOrder[A].on(_.a)
PartialOrder.by(_.a)
implicit def hasPartialOrderArbitrary[A: Arbitrary]: Arbitrary[HasPartialOrder[A]] =
Arbitrary(arbitrary[A].map(HasPartialOrder(_)))
implicit def hasCogen[A: Cogen]: Cogen[HasPartialOrder[A]] =
Expand Down
22 changes: 2 additions & 20 deletions kernel/src/main/scala/cats/kernel/Eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ 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)

/**
* Constructs a new `Eq` instance for type `B` where 2 elements are
* equivalent iff `eqv(f(x), f(y))`.
*/
def on[@sp B](f: B => A): Eq[B] = Eq.by[B, A](f)(self)

/**
* Return an Eq that gives the result of the and of this and that
* note this is idempotent
*/
def and(that: Eq[A]): Eq[A] = Eq.and(self, that)

/**
* Return an Eq that gives the result of the or of this and that
* Note this is idempotent
*/
def or(that: Eq[A]): Eq[A] = Eq.or(self, that)
}

abstract class EqFunctions[E[T] <: Eq[T]] {
Expand Down Expand Up @@ -124,7 +106,7 @@ object Eq extends EqFunctions[Eq] {
*/
def allEqualBoundedSemilattice[A]: BoundedSemilattice[Eq[A]] = new BoundedSemilattice[Eq[A]] {
def empty = allEqual[A]
def combine(e1: Eq[A], e2: Eq[A]): Eq[A] = e1.and(e2)
def combine(e1: Eq[A], e2: Eq[A]): Eq[A] = Eq.and(e1, e2)
override def combineAllOption(es: TraversableOnce[Eq[A]]): Option[Eq[A]] =
if (es.isEmpty) None
else {
Expand All @@ -140,7 +122,7 @@ object Eq extends EqFunctions[Eq] {
* checks that at least one equality check passes
*/
def anyEqualSemilattice[A]: Semilattice[Eq[A]] = new Semilattice[Eq[A]] {
def combine(e1: Eq[A], e2: Eq[A]): Eq[A] = e1.or(e2)
def combine(e1: Eq[A], e2: Eq[A]): Eq[A] = Eq.or(e1, e2)
override def combineAllOption(es: TraversableOnce[Eq[A]]): Option[Eq[A]] =
if (es.isEmpty) None
else {
Expand Down
22 changes: 1 addition & 21 deletions kernel/src/main/scala/cats/kernel/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ trait Order[@sp A] extends Any with PartialOrder[A] { self =>
*/
def max(x: A, y: A): A = if (gt(x, y)) x else y

/**
* Defines an order on `B` by mapping `B` to `A` using `f` and using `A`s
* order to order `B`.
*/
override def on[@sp B](f: B => A): Order[B] = Order.by[B, A](f)(self)

/**
* Defines an ordering on `A` where all arrows switch direction.
*/
override def reverse: Order[A] = Order.reverse(self)

// The following may be overridden for performance:

/**
Expand Down Expand Up @@ -95,15 +84,6 @@ trait Order[@sp A] extends Any with PartialOrder[A] { self =>
override def gt(x: A, y: A): Boolean =
compare(x, y) > 0

/**
* Returns a new `Order[A]` instance that first compares by the original
* `Order` instance and uses the provided `Order` instance to "break ties".
*
* That is, `x.whenEqual(y)` creates an `Order` that first orders by `x` and
* then (if two elements are equal) falls back to `y` for the comparison.
*/
def whenEqual(o: Order[A]): Order[A] = Order.whenEqual(self, o)

/**
* Convert a `Order[A]` to a `scala.math.Ordering[A]`
* instance.
Expand Down Expand Up @@ -214,7 +194,7 @@ object Order extends OrderFunctions[Order] {
def whenEqualMonoid[A]: Monoid[Order[A]] with Band[Order[A]] =
new Monoid[Order[A]] with Band[Order[A]] {
val empty: Order[A] = allEqual[A]
def combine(x: Order[A], y: Order[A]): Order[A] = x whenEqual y
def combine(x: Order[A], y: Order[A]): Order[A] = Order.whenEqual(x, y)
}

def fromOrdering[A](implicit ev: Ordering[A]): Order[A] =
Expand Down
11 changes: 0 additions & 11 deletions kernel/src/main/scala/cats/kernel/PartialOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,6 @@ trait PartialOrder[@sp A] extends Any with Eq[A] { self =>
else None
}

/**
* Defines a partial order on `B` by mapping `B` to `A` using `f`
* and using `A`s order to order `B`.
*/
override def on[@sp B](f: B => A): PartialOrder[B] = PartialOrder.by[B, A](f)(self)

/**
* Defines a partial order on `A` where all arrows switch direction.
*/
def reverse: PartialOrder[A] = PartialOrder.reverse(self)

// The following may be overridden for performance:

/**
Expand Down
8 changes: 4 additions & 4 deletions laws/src/main/scala/cats/laws/discipline/Eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ object eq {
* Create an approximation of Eq[Semigroup[A]] by generating values for A
* and comparing the application of the two combine functions.
*/
implicit def catsLawsEqForSemigroup[A](implicit arbAA: Arbitrary[(A, A)], eqA: Eq[A]): Eq[Semigroup[A]] =
catsLawsEqForFn1[(A, A), A].on(f =>
Function.tupled((x, y) => f.combine(x, y))
)
implicit def catsLawsEqForSemigroup[A](implicit arbAA: Arbitrary[(A, A)], eqA: Eq[A]): Eq[Semigroup[A]] = {
val instance: Eq[((A, A)) => A] = catsLawsEqForFn1[(A, A), A]
Eq.by[Semigroup[A], ((A, A)) => A]( f => Function.tupled((x, y) => f.combine(x, y)))(instance)
}

implicit def catsLawsEqForCommutativeSemigroup[A](implicit arbAA: Arbitrary[(A, A)], eqA: Eq[A]): Eq[CommutativeSemigroup[A]] = {
implicit val eqABool: Eq[(A, Boolean)] = Eq.instance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ object CsvCodecInvariantMonoidalSuite {
implicit val arbNumericSystemCodec: Arbitrary[CsvCodec[Int]] =
Arbitrary(Gen.choose(2, 16).map(numericSystemCodec))

implicit def csvCodecsEq[A](implicit a: Arbitrary[A], e: Eq[A]): Eq[CsvCodec[A]] =
catsLawsEqForFn1[A, CSV].on[CsvCodec[A]](_.write) and catsLawsEqForFn1[CSV, (Option[A], CSV)].on[CsvCodec[A]](_.read)
implicit def csvCodecsEq[A](implicit a: Arbitrary[A], e: Eq[A]): Eq[CsvCodec[A]] = {
val writeEq: Eq[CsvCodec[A]] = Eq.by[CsvCodec[A], A => CSV](_.write)(catsLawsEqForFn1[A, CSV])
val readEq: Eq[CsvCodec[A]] = Eq.by[CsvCodec[A], CSV => (Option[A], CSV)](_.read)(catsLawsEqForFn1[CSV, (Option[A], CSV)])
Eq.and(writeEq, readEq)
}
}

class CsvCodecInvariantMonoidalSuite extends CatsSuite {
Expand Down
6 changes: 3 additions & 3 deletions tests/src/test/scala/cats/tests/ListWrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import org.scalacheck.Arbitrary.arbitrary
final case class ListWrapper[A](list: List[A]) extends AnyVal

object ListWrapper {
def order[A:Order]: Order[ListWrapper[A]] = Order[List[A]].on[ListWrapper[A]](_.list)
def order[A:Order]: Order[ListWrapper[A]] = Order.by(_.list)

def partialOrder[A:PartialOrder]: PartialOrder[ListWrapper[A]] = PartialOrder[List[A]].on[ListWrapper[A]](_.list)
def partialOrder[A:PartialOrder]: PartialOrder[ListWrapper[A]] = PartialOrder.by(_.list)

def eqv[A : Eq]: Eq[ListWrapper[A]] = Eq[List[A]].on[ListWrapper[A]](_.list)
def eqv[A : Eq]: Eq[ListWrapper[A]] = Eq.by(_.list)

val traverse: Traverse[ListWrapper] = {
val F = Traverse[List]
Expand Down

0 comments on commit cedb6ee

Please sign in to comment.