From 6c3410bfdd2c9b8ed3b05bba69974006e0a67411 Mon Sep 17 00:00:00 2001 From: Andy Scott Date: Fri, 8 Dec 2017 14:02:27 -0800 Subject: [PATCH] Add high priority IorT parallel instance that leverages parallel effect --- core/src/main/scala/cats/data/IorT.scala | 25 +++++++++- .../test/scala/cats/tests/ParallelSuite.scala | 50 ++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/cats/data/IorT.scala b/core/src/main/scala/cats/data/IorT.scala index e36dc7a97a..3384f6a025 100644 --- a/core/src/main/scala/cats/data/IorT.scala +++ b/core/src/main/scala/cats/data/IorT.scala @@ -156,6 +156,7 @@ object IorT extends IorTInstances { * scala> import cats.implicits._ * scala> IorT.leftT[Option, Int]("err") * res0: cats.data.IorT[Option,String,Int] = IorT(Some(Left(err))) + * }}} */ final def leftT[F[_], B]: LeftTPartiallyApplied[F, B] = new LeftTPartiallyApplied[F, B] @@ -413,6 +414,27 @@ private[data] abstract class IorTInstances extends IorTInstances1 { implicit def catsDataMonoidForIorT[F[_], A, B](implicit F: Monoid[F[Ior[A, B]]]): Monoid[IorT[F, A, B]] = new IorTMonoid[F, A, B] { val F0: Monoid[F[Ior[A, B]]] = F } + + implicit def catsDataParallelForIorTWithParallelEffect[M[_], F[_], E] + (implicit P: Parallel[M, F], E: Semigroup[E]): Parallel[IorT[M, E, ?], IorT[F, E, ?]] = new Parallel[IorT[M, E, ?], IorT[F, E, ?]] + { + val parallel: IorT[M, E, ?] ~> IorT[F, E, ?] = λ[IorT[M, E, ?] ~> IorT[F, E, ?]](fm => IorT(P.parallel(fm.value))) + val sequential: IorT[F, E, ?] ~> IorT[M, E, ?] = λ[IorT[F, E, ?] ~> IorT[M, E, ?]](ff => IorT(P.sequential(ff.value))) + + private[this] val FA: Applicative[F] = P.applicative + private[this] val IorA: Applicative[Ior[E, ?]] = Parallel[Ior[E, ?], Ior[E, ?]].applicative + + val applicative: Applicative[IorT[F, E, ?]] = new Applicative[IorT[F, E, ?]] { + def pure[A](a: A): IorT[F, E, A] = IorT.pure(a)(FA) + def ap[A, B](ff: IorT[F, E, A => B])(fa: IorT[F, E, A]): IorT[F, E, B] = + IorT(FA.map2(ff.value, fa.value)((f, a) => IorA.ap(f)(a))) + } + + lazy val monad: Monad[IorT[M, E, ?]] = { + implicit def underlyingMonadM: Monad[M] = P.monad + Monad[IorT[M, E, ?]] + } + } } private[data] abstract class IorTInstances1 extends IorTInstances2 { @@ -428,10 +450,9 @@ private[data] abstract class IorTInstances1 extends IorTInstances2 { val F0: Monad[F] = F } - implicit def parallelForIorT[F[_], E] + implicit def catsDataParallelForIorTWithSequentialEffect[F[_], E] (implicit F: Monad[F], E: Semigroup[E]): Parallel[IorT[F, E, ?], IorT[F, E, ?]] = new Parallel[IorT[F, E, ?], IorT[F, E, ?]] { - private[this] val identityK: IorT[F, E, ?] ~> IorT[F, E, ?] = FunctionK.id private[this] val underlyingParallel: Parallel[Ior[E, ?], Ior[E, ?]] = Parallel[Ior[E, ?], Ior[E, ?]] diff --git a/tests/src/test/scala/cats/tests/ParallelSuite.scala b/tests/src/test/scala/cats/tests/ParallelSuite.scala index 844431ff25..8796ff7f0c 100644 --- a/tests/src/test/scala/cats/tests/ParallelSuite.scala +++ b/tests/src/test/scala/cats/tests/ParallelSuite.scala @@ -169,9 +169,57 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest { } } + test("IorT leverages parallel effect instances when it exists") { + case class Marker(value: String) extends Exception("marker") { + override def fillInStackTrace: Throwable = null + } + + def checkMarker[A](f: => A): Option[String] = + try { f; None } catch { + case marker: Marker => marker.value.some + case _: Throwable => None + } + + final case class Effect[A](value: A) + val monadInstance: Monad[Effect] = new Monad[Effect] { + def pure[A](a: A): Effect[A] = Effect(a) + def flatMap[A, B](fa: Effect[A])(f: A => Effect[B]): Effect[B] = throw Marker("sequential") + def tailRecM[A, B](a: A)(f: A => Effect[Either[A, B]]): Effect[B] = ??? + } + val parallelInstance: Parallel[Effect, Effect] = new Parallel[Effect, Effect] { + def parallel: Effect ~> Effect = arrow.FunctionK.id + def sequential: Effect ~> Effect = arrow.FunctionK.id + + def applicative: Applicative[Effect] = new Applicative[Effect] { + def pure[A](a: A): Effect[A] = Effect(a) + def ap[A, B](ff: Effect[A => B])(fa: Effect[A]): Effect[B] = throw Marker("parallel") + } + def monad: Monad[Effect] = monadInstance + } + + val iorts: List[IorT[Effect, String, Int]] = List( + IorT.leftT("hello")(monadInstance), + IorT.bothT(" world", 404)(monadInstance), + IorT.rightT(123)(monadInstance)) + + val resultSansInstance = { + implicit val ev0 = monadInstance + checkMarker(iorts.parSequence) + } + val resultWithInstance = { + implicit val ev0 = monadInstance + implicit val ev1 = parallelInstance + checkMarker(iorts.parSequence) + } + + resultSansInstance should === ("sequential".some) + resultWithInstance should === ("parallel".some) + } + checkAll("Parallel[Either[String, ?], Validated[String, ?]]", ParallelTests[Either[String, ?], Validated[String, ?]].parallel[Int, String]) checkAll("Parallel[Ior[String, ?], Ior[String, ?]]", ParallelTests[Ior[String, ?], Ior[String, ?]].parallel[Int, String]) - checkAll("Parallel[IorT[F, String, ?], IorT[F, String, ?]]", ParallelTests[IorT[Option, String, ?], IorT[Option, String, ?]].parallel[Int, String]) + checkAll("Parallel[IorT[F, String, ?], IorT[F, String, ?]] with parallel effect", ParallelTests[IorT[Either[String, ?], String, ?], IorT[Validated[String, ?], String, ?]].parallel[Int, String]) + checkAll("Parallel[IorT[F, String, ?], IorT[F, String, ?]] with sequential effect", ParallelTests[IorT[Option, String, ?], IorT[Option, String, ?]].parallel[Int, String]) checkAll("Parallel[OptionT[M, ?], Nested[F, Option, ?]]", ParallelTests[OptionT[Either[String, ?], ?], Nested[Validated[String, ?], Option, ?]].parallel[Int, String]) checkAll("Parallel[EitherT[M, String, ?], Nested[F, Validated[String, ?], ?]]", ParallelTests[EitherT[Either[String, ?], String, ?], Nested[Validated[String, ?], Validated[String, ?], ?]].parallel[Int, String]) checkAll("Parallel[EitherT[Option, String, ?], Nested[Option, Validated[String, ?], ?]]", ParallelTests[EitherT[Option, String, ?], Nested[Option, Validated[String, ?], ?]].parallel[Int, String])