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

added TFunctor #1815

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ object EitherT extends EitherTInstances {

private[data] abstract class EitherTInstances extends EitherTInstances1 {


implicit def catsDataTFunctorFoEitherT[A]: TFunctor[EitherT[?[_], A, ?]] = new TFunctor[EitherT[?[_], A, ?]] {
def mapNT[F[_], G[_], B](h: EitherT[F, A, B])(f: F ~> G): EitherT[G, A, B] = EitherT(f(h.value))
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/data/Func.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ object Func extends FuncInstances {
}

private[data] abstract class FuncInstances extends FuncInstances0 {

implicit def catsDataTFunctorFoFunc[A]: TFunctor[Func[?[_], A, ?]] = new TFunctor[Func[?[_], A, ?]] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing "r" in "foFunc", similarly in "foNested".

def mapNT[F[_], G[_], B](h: Func[F, A, B])(f: F ~> G): Func[G, A, B] = Func.func(h.run.andThen(f.apply))
}

implicit def catsDataApplicativeForFunc[F[_], C](implicit FF: Applicative[F]): Applicative[λ[α => Func[F, C, α]]] =
new FuncApplicative[F, C] {
def F: Applicative[F] = FF
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/Nested.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ final case class Nested[F[_], G[_], A](value: F[G[A]])
object Nested extends NestedInstances

private[data] sealed abstract class NestedInstances extends NestedInstances0 {


implicit def catsDataTFunctorFoNested[I[_]]: TFunctor[Nested[?[_], I, ?]] = new TFunctor[Nested[?[_], I, ?]] {
def mapNT[F[_], G[_], A](n: Nested[F, I, A])(f: F ~> G): Nested[G, I, A] = Nested(f(n.value))
}

implicit def catsDataEqForNested[F[_], G[_], A](implicit FGA: Eq[F[G[A]]]): Eq[Nested[F, G, A]] =
FGA.on(_.value)

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/data/OneAnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ final case class OneAnd[F[_], A](head: A, tail: F[A]) {

private[data] sealed trait OneAndInstances extends OneAndLowPriority3 {

implicit val catsDataTFunctorForOneAnd: TFunctor[OneAnd] = new TFunctor[OneAnd] {
def mapNT[F[_], G[_], A](o: OneAnd[F, A])(f: F ~> G): OneAnd[G, A] = OneAnd(o.head, f(o.tail))
}

implicit def catsDataEqForOneAnd[A, F[_]](implicit A: Eq[A], FA: Eq[F[A]]): Eq[OneAnd[F, A]] =
new Eq[OneAnd[F, A]]{
def eqv(x: OneAnd[F, A], y: OneAnd[F, A]): Boolean = x === y
Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/scala/cats/tests/EitherTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import cats.kernel.laws.{GroupLaws, OrderLaws}
class EitherTTests extends CatsSuite {
implicit val iso = CartesianTests.Isomorphisms.invariant[EitherT[ListWrapper, String, ?]](EitherT.catsDataFunctorForEitherT(ListWrapper.functor))

checkAll("EitherT[?[_], String, ?]", TFunctorTests[EitherT[?[_], String, ?]].tfunctor[List, Vector, Option, Int])
checkAll("TFunctor[EitherT[?[_], String, ?]]", SerializableTests.serializable(TFunctor[EitherT[?[_], String, ?]]))

{
checkAll("EitherT[Option, ListWrapper[String], ?]", SemigroupKTests[EitherT[Option, ListWrapper[String], ?]].semigroupK[Int])
checkAll("SemigroupK[EitherT[Option, ListWrapper[String], ?]]", SerializableTests.serializable(SemigroupK[EitherT[Option, ListWrapper[String], ?]]))
Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/scala/cats/tests/FuncTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class FuncTests extends CatsSuite {

implicit val iso = CartesianTests.Isomorphisms.invariant[Func[Option, Int, ?]]

checkAll("Func[?[_], String, ?]", TFunctorTests[Func[?[_], String, ?]].tfunctor[List, Vector, Option, Int])
checkAll("TFunctor[Func[?[_], String, ?]]", SerializableTests.serializable(TFunctor[Func[?[_], String, ?]]))

checkAll("Func[Option, Int, Int]", CartesianTests[Func[Option, Int, ?]].cartesian[Int, Int, Int])
checkAll("Cartesian[Func[Option, Int, ?]]", SerializableTests.serializable(Cartesian[Func[Option, Int, ?]]))

Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/scala/cats/tests/NestedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class NestedTests extends CatsSuite {
implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(minSuccessful = 20, sizeRange = 5)

checkAll("Nested[?[_], Option, ?]", TFunctorTests[Nested[?[_], Option, ?]].tfunctor[List, Vector, Option, Int])
checkAll("TFunctor[Nested[?[_], Option, ?]]", SerializableTests.serializable(TFunctor[Nested[?[_], Option, ?]]))

{
// Invariant composition
implicit val instance = ListWrapper.invariant
Expand Down
6 changes: 5 additions & 1 deletion tests/src/test/scala/cats/tests/OneAndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import cats.kernel.laws.{GroupLaws, OrderLaws}

import cats.instances.stream._
import cats.data.{NonEmptyStream, OneAnd}
import cats.laws.discipline.{ComonadTests, FunctorTests, SemigroupKTests, FoldableTests, MonadTests, SerializableTests, CartesianTests, TraverseTests, NonEmptyTraverseTests, ReducibleTests}
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._

class OneAndTests extends CatsSuite {
// Lots of collections here.. telling ScalaCheck to calm down a bit
implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(minSuccessful = 20, sizeRange = 5)


checkAll("OneAnd", TFunctorTests[OneAnd].tfunctor[List, Vector, Option, Int])
checkAll("TFunctor[OneAnd]", SerializableTests.serializable(TFunctor[OneAnd]))

checkAll("OneAnd[Stream, Int]", OrderLaws[OneAnd[Stream, Int]].eqv)

checkAll("OneAnd[Stream, Int] with Option", NonEmptyTraverseTests[OneAnd[Stream, ?]].nonEmptyTraverse[Option, Int, Int, Int, Int, Option, Option])
Expand Down