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

Get FunctorFilter on par with the other MTL typeclasses #2594

Merged
merged 3 commits into from
Nov 19, 2018
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
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/data/IndexedStateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ sealed abstract private[data] class IndexedStateTInstances extends IndexedStateT
def defer[A](fa: => IndexedStateT[F, SA, SB, A]): IndexedStateT[F, SA, SB, A] =
IndexedStateT.applyF(F.defer(fa.runF))
}

implicit def catsDataFunctorFilterForIndexedStateT[F[_], SA, SB](
implicit
ev1: Monad[F],
ev2: FunctorFilter[F]
): FunctorFilter[IndexedStateT[F, SA, SB, ?]] =
new IndexedStateTFunctorFilter[F, SA, SB] {
val F0 = ev1
val FF = ev2
}
}

sealed abstract private[data] class IndexedStateTInstances1 extends IndexedStateTInstances2 {
Expand Down Expand Up @@ -475,3 +485,15 @@ sealed abstract private[data] class IndexedStateTMonadError[F[_], S, E]
def handleErrorWith[A](fa: IndexedStateT[F, S, S, A])(f: E => IndexedStateT[F, S, S, A]): IndexedStateT[F, S, S, A] =
IndexedStateT(s => F.handleErrorWith(fa.run(s))(e => f(e).run(s)))
}

private[this] trait IndexedStateTFunctorFilter[F[_], SA, SB] extends FunctorFilter[IndexedStateT[F, SA, SB, ?]] {

implicit def F0: Monad[F]
def FF: FunctorFilter[F]

def functor: Functor[IndexedStateT[F, SA, SB, ?]] =
IndexedStateT.catsDataFunctorForIndexedStateT(FF.functor)

def mapFilter[A, B](fa: IndexedStateT[F, SA, SB, A])(f: A => Option[B]): IndexedStateT[F, SA, SB, B] =
fa.flatMapF(a => FF.mapFilter(F0.pure(a))(f))
}
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/data/Kleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ sealed abstract private[data] class KleisliInstances extends KleisliInstances0 {
}
}
}

implicit def catsDataFunctorFilterForKleisli[F[_], A](
implicit ev: FunctorFilter[F]
): FunctorFilter[Kleisli[F, A, ?]] =
new KleisliFunctorFilter[F, A] { val FF = ev }
}

sealed abstract private[data] class KleisliInstances0 extends KleisliInstances0_5 {
Expand Down Expand Up @@ -503,3 +508,15 @@ private trait KleisliDistributive[F[_], R] extends Distributive[Kleisli[F, R, ?]

def map[A, B](fa: Kleisli[F, R, A])(f: A => B): Kleisli[F, R, B] = fa.map(f)
}

private[this] trait KleisliFunctorFilter[F[_], R] extends FunctorFilter[Kleisli[F, R, ?]] {

def FF: FunctorFilter[F]

def functor: Functor[Kleisli[F, R, ?]] = Kleisli.catsDataFunctorForKleisli(FF.functor)

def mapFilter[A, B](fa: Kleisli[F, R, A])(f: A => Option[B]): Kleisli[F, R, B] =
Kleisli[F, R, B] { r =>
FF.mapFilter(fa.run(r))(f)
}
}
12 changes: 12 additions & 0 deletions tests/src/test/scala/cats/tests/IndexedStateTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ class IndexedStateTSuite extends CatsSuite {
Functor[IndexedStateT[ListWrapper, String, Int, ?]]
}

{
implicit val F0 = ListWrapper.monad
implicit val FF = ListWrapper.functorFilter

checkAll("IndexedStateT[ListWrapper, String, Int, ?]",
FunctorFilterTests[IndexedStateT[ListWrapper, String, Int, ?]].functorFilter[Int, Int, Int])
checkAll("FunctorFilter[IndexedStateT[ListWrapper, String, Int, ?]]",
SerializableTests.serializable(FunctorFilter[IndexedStateT[ListWrapper, String, Int, ?]]))

FunctorFilter[IndexedStateT[ListWrapper, String, Int, ?]]
}

{
implicit val F: Monad[ListWrapper] = ListWrapper.monad
implicit val FS: Contravariant[IndexedStateT[ListWrapper, ?, Int, Int]] =
Expand Down
13 changes: 12 additions & 1 deletion tests/src/test/scala/cats/tests/KleisliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests

import cats.Contravariant
import cats.arrow._
import cats.data.{Const, EitherT, Kleisli, Reader}
import cats.data.{Const, EitherT, Kleisli, Reader, ReaderT}
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.eq._
Expand Down Expand Up @@ -141,6 +141,17 @@ class KleisliSuite extends CatsSuite {
checkAll("Functor[Kleisli[Option, Int, ?]]", SerializableTests.serializable(Functor[Kleisli[Option, Int, ?]]))
}

{
implicit val FF = ListWrapper.functorFilter

checkAll("Kleisli[ListWrapper, Int, ?]",
FunctorFilterTests[Kleisli[ListWrapper, Int, ?]].functorFilter[Int, Int, Int])
checkAll("FunctorFilter[Kleisli[ListWrapper, Int, ?]]",
SerializableTests.serializable(FunctorFilter[Kleisli[ListWrapper, Int, ?]]))

FunctorFilter[ReaderT[ListWrapper, Int, ?]]
}

{
checkAll("Kleisli[Function0, Int, ?]",
DistributiveTests[Kleisli[Function0, Int, ?]].distributive[Int, Int, Int, Option, Id])
Expand Down