-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
move instances into separate trait #1659
Changes from all commits
a644aaf
4775bd8
4acff56
fea3b25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cats | ||
package instances | ||
|
||
trait EqInstances { | ||
implicit val catsContravariantCartesianEq: ContravariantCartesian[Eq] = new ContravariantCartesian[Eq] { | ||
def contramap[A, B](fa: Eq[A])(fn: B => A): Eq[B] = fa.on(fn) | ||
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) } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cats | ||
package instances | ||
|
||
trait MonoidInstances { | ||
|
||
implicit val catsInvariantMonoidalMonoid: InvariantMonoidal[Monoid] = new InvariantMonoidal[Monoid] { | ||
def product[A, B](fa: Monoid[A], fb: Monoid[B]): Monoid[(A, B)] = new Monoid[(A, B)] { | ||
val empty = fa.empty -> fb.empty | ||
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2) | ||
} | ||
|
||
def imap[A, B](fa: Monoid[A])(f: A => B)(g: B => A): Monoid[B] = new Monoid[B] { | ||
val empty = f(fa.empty) | ||
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y))) | ||
override def combineAll(bs: TraversableOnce[B]): B = | ||
f(fa.combineAll(bs.map(g))) | ||
} | ||
|
||
def pure[A](a: A): Monoid[A] = new Monoid[A] { | ||
val empty = a | ||
def combine(x: A, y: A): A = a | ||
override def combineAll(as: TraversableOnce[A]): A = a | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cats | ||
package instances | ||
|
||
import cats.functor.Contravariant | ||
|
||
trait OrderInstances { | ||
|
||
implicit val catsFunctorContravariantForOrder: Contravariant[Order] = | ||
new Contravariant[Order] { | ||
/** Derive an `Order` for `B` given an `Order[A]` and a function `B => A`. | ||
* | ||
* 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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cats | ||
package instances | ||
|
||
import cats.functor.Contravariant | ||
|
||
trait PartialOrderInstances { | ||
implicit val catsFunctorContravariantForPartialOrder: Contravariant[PartialOrder] = | ||
new Contravariant[PartialOrder] { | ||
/** Derive a `PartialOrder` for `B` given a `PartialOrder[A]` and a function `B => A`. | ||
* | ||
* 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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cats | ||
package instances | ||
|
||
trait SemigroupInstances { | ||
|
||
implicit val catsInvariantMonoidalSemigroup: InvariantMonoidal[Semigroup] = new InvariantMonoidal[Semigroup] { | ||
def product[A, B](fa: Semigroup[A], fb: Semigroup[B]): Semigroup[(A, B)] = new Semigroup[(A, B)] { | ||
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2) | ||
} | ||
|
||
def imap[A, B](fa: Semigroup[A])(f: A => B)(g: B => A): Semigroup[B] = new Semigroup[B] { | ||
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y))) | ||
override def combineAllOption(bs: TraversableOnce[B]): Option[B] = | ||
fa.combineAllOption(bs.map(g)).map(f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line was not covered in the original code, which is a bit surprising to me. We can probably address it in a different PR. Just want to keep a note here. |
||
} | ||
|
||
def pure[A](a: A): Semigroup[A] = new Semigroup[A] { | ||
def combine(x: A, y: A): A = a | ||
override def combineAllOption(as: TraversableOnce[A]): Option[A] = | ||
if (as.isEmpty) None else Some(a) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line was not covered in the original code. We can probably address it in a different PR. Just want to keep a note here. |
||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cats | ||
package tests | ||
|
||
import org.scalatest._ | ||
|
||
import cats.functor._ | ||
|
||
class EqTests extends FunSuite { | ||
{ | ||
import cats.implicits._ | ||
Invariant[Eq] | ||
Contravariant[Eq] | ||
Cartesian[Eq] | ||
ContravariantCartesian[Eq] | ||
} | ||
|
||
{ | ||
import cats.instances.eq._ | ||
Invariant[Eq] | ||
Contravariant[Eq] | ||
Cartesian[Eq] | ||
ContravariantCartesian[Eq] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cats | ||
package tests | ||
|
||
import org.scalatest._ | ||
|
||
import cats.functor._ | ||
|
||
class MonoidTests extends FunSuite { | ||
{ | ||
import cats.implicits._ | ||
Invariant[Monoid] | ||
Cartesian[Monoid] | ||
InvariantMonoidal[Monoid] | ||
} | ||
|
||
{ | ||
import cats.instances.monoid._ | ||
Invariant[Monoid] | ||
Cartesian[Monoid] | ||
InvariantMonoidal[Monoid] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cats | ||
package tests | ||
|
||
import cats.functor._ | ||
|
||
import org.scalatest._ | ||
|
||
class OrderTests extends FunSuite { | ||
{ | ||
import cats.implicits._ | ||
Invariant[Order] | ||
Contravariant[Order] | ||
} | ||
|
||
{ | ||
import cats.instances.order._ | ||
Invariant[Order] | ||
Contravariant[Order] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cats | ||
package tests | ||
|
||
import org.scalatest._ | ||
|
||
import cats.functor._ | ||
|
||
class PartialOrderTests extends FunSuite { | ||
{ | ||
import cats.implicits._ | ||
Invariant[PartialOrder] | ||
Contravariant[PartialOrder] | ||
} | ||
|
||
{ | ||
import cats.instances.partialOrder._ | ||
Invariant[PartialOrder] | ||
Contravariant[PartialOrder] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cats | ||
package tests | ||
|
||
import org.scalatest._ | ||
|
||
import cats.functor._ | ||
|
||
class SemigroupTests extends FunSuite { | ||
{ | ||
import cats.implicits._ | ||
Invariant[Semigroup] | ||
Cartesian[Semigroup] | ||
InvariantMonoidal[Semigroup] | ||
} | ||
|
||
{ | ||
import cats.instances.semigroup._ | ||
Invariant[Semigroup] | ||
Cartesian[Semigroup] | ||
InvariantMonoidal[Semigroup] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line was not covered in the original code, which is a bit surprising to me. We can probably address it in a different PR. Just want to keep a note here.