Skip to content

Commit 60d3c12

Browse files
andyscottkailuowang
authored andcommitted
Add parallel instance for IorT (#2060)
* Add parallel instance for IorT * Add high priority IorT parallel instance that leverages parallel effect
1 parent 1bc8b11 commit 60d3c12

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

core/src/main/scala/cats/data/IorT.scala

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cats
22
package data
33

4+
import cats.arrow.FunctionK
45
import cats.syntax.either._
56
import cats.syntax.option._
67

@@ -155,6 +156,7 @@ object IorT extends IorTInstances {
155156
* scala> import cats.implicits._
156157
* scala> IorT.leftT[Option, Int]("err")
157158
* res0: cats.data.IorT[Option,String,Int] = IorT(Some(Left(err)))
159+
158160
* }}}
159161
*/
160162
final def leftT[F[_], B]: LeftTPartiallyApplied[F, B] = new LeftTPartiallyApplied[F, B]
@@ -412,6 +414,27 @@ private[data] abstract class IorTInstances extends IorTInstances1 {
412414

413415
implicit def catsDataMonoidForIorT[F[_], A, B](implicit F: Monoid[F[Ior[A, B]]]): Monoid[IorT[F, A, B]] =
414416
new IorTMonoid[F, A, B] { val F0: Monoid[F[Ior[A, B]]] = F }
417+
418+
implicit def catsDataParallelForIorTWithParallelEffect[M[_], F[_], E]
419+
(implicit P: Parallel[M, F], E: Semigroup[E]): Parallel[IorT[M, E, ?], IorT[F, E, ?]] = new Parallel[IorT[M, E, ?], IorT[F, E, ?]]
420+
{
421+
val parallel: IorT[M, E, ?] ~> IorT[F, E, ?] = λ[IorT[M, E, ?] ~> IorT[F, E, ?]](fm => IorT(P.parallel(fm.value)))
422+
val sequential: IorT[F, E, ?] ~> IorT[M, E, ?] = λ[IorT[F, E, ?] ~> IorT[M, E, ?]](ff => IorT(P.sequential(ff.value)))
423+
424+
private[this] val FA: Applicative[F] = P.applicative
425+
private[this] val IorA: Applicative[Ior[E, ?]] = Parallel[Ior[E, ?], Ior[E, ?]].applicative
426+
427+
val applicative: Applicative[IorT[F, E, ?]] = new Applicative[IorT[F, E, ?]] {
428+
def pure[A](a: A): IorT[F, E, A] = IorT.pure(a)(FA)
429+
def ap[A, B](ff: IorT[F, E, A => B])(fa: IorT[F, E, A]): IorT[F, E, B] =
430+
IorT(FA.map2(ff.value, fa.value)((f, a) => IorA.ap(f)(a)))
431+
}
432+
433+
lazy val monad: Monad[IorT[M, E, ?]] = {
434+
implicit def underlyingMonadM: Monad[M] = P.monad
435+
Monad[IorT[M, E, ?]]
436+
}
437+
}
415438
}
416439

417440
private[data] abstract class IorTInstances1 extends IorTInstances2 {
@@ -426,6 +449,26 @@ private[data] abstract class IorTInstances1 extends IorTInstances2 {
426449
val A0: Semigroup[A] = A
427450
val F0: Monad[F] = F
428451
}
452+
453+
implicit def catsDataParallelForIorTWithSequentialEffect[F[_], E]
454+
(implicit F: Monad[F], E: Semigroup[E]): Parallel[IorT[F, E, ?], IorT[F, E, ?]] = new Parallel[IorT[F, E, ?], IorT[F, E, ?]]
455+
{
456+
private[this] val identityK: IorT[F, E, ?] ~> IorT[F, E, ?] = FunctionK.id
457+
private[this] val underlyingParallel: Parallel[Ior[E, ?], Ior[E, ?]] =
458+
Parallel[Ior[E, ?], Ior[E, ?]]
459+
460+
def parallel: IorT[F, E, ?] ~> IorT[F, E, ?] = identityK
461+
def sequential: IorT[F, E, ?] ~> IorT[F, E, ?] = identityK
462+
463+
val applicative: Applicative[IorT[F, E, ?]] = new Applicative[IorT[F, E, ?]] {
464+
def pure[A](a: A): IorT[F, E, A] = IorT.pure(a)
465+
def ap[A, B](ff: IorT[F, E, A => B])(fa: IorT[F, E, A]): IorT[F, E, B] =
466+
IorT(F.map2(ff.value, fa.value)((f, a) => underlyingParallel.applicative.ap(f)(a)))
467+
}
468+
469+
lazy val monad: Monad[IorT[F, E, ?]] = Monad[IorT[F, E, ?]]
470+
}
471+
429472
}
430473

431474
private[data] abstract class IorTInstances2 extends IorTInstances3 {

tests/src/test/scala/cats/tests/ParallelSuite.scala

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,57 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest {
169169
}
170170
}
171171

172+
test("IorT leverages parallel effect instances when it exists") {
173+
case class Marker(value: String) extends Exception("marker") {
174+
override def fillInStackTrace: Throwable = null
175+
}
176+
177+
def checkMarker[A](f: => A): Option[String] =
178+
try { f; None } catch {
179+
case marker: Marker => marker.value.some
180+
case _: Throwable => None
181+
}
182+
183+
final case class Effect[A](value: A)
184+
val monadInstance: Monad[Effect] = new Monad[Effect] {
185+
def pure[A](a: A): Effect[A] = Effect(a)
186+
def flatMap[A, B](fa: Effect[A])(f: A => Effect[B]): Effect[B] = throw Marker("sequential")
187+
def tailRecM[A, B](a: A)(f: A => Effect[Either[A, B]]): Effect[B] = ???
188+
}
189+
val parallelInstance: Parallel[Effect, Effect] = new Parallel[Effect, Effect] {
190+
def parallel: Effect ~> Effect = arrow.FunctionK.id
191+
def sequential: Effect ~> Effect = arrow.FunctionK.id
192+
193+
def applicative: Applicative[Effect] = new Applicative[Effect] {
194+
def pure[A](a: A): Effect[A] = Effect(a)
195+
def ap[A, B](ff: Effect[A => B])(fa: Effect[A]): Effect[B] = throw Marker("parallel")
196+
}
197+
def monad: Monad[Effect] = monadInstance
198+
}
199+
200+
val iorts: List[IorT[Effect, String, Int]] = List(
201+
IorT.leftT("hello")(monadInstance),
202+
IorT.bothT(" world", 404)(monadInstance),
203+
IorT.rightT(123)(monadInstance))
204+
205+
val resultSansInstance = {
206+
implicit val ev0 = monadInstance
207+
checkMarker(iorts.parSequence)
208+
}
209+
val resultWithInstance = {
210+
implicit val ev0 = monadInstance
211+
implicit val ev1 = parallelInstance
212+
checkMarker(iorts.parSequence)
213+
}
214+
215+
resultSansInstance should === ("sequential".some)
216+
resultWithInstance should === ("parallel".some)
217+
}
218+
172219
checkAll("Parallel[Either[String, ?], Validated[String, ?]]", ParallelTests[Either[String, ?], Validated[String, ?]].parallel[Int, String])
173220
checkAll("Parallel[Ior[String, ?], Ior[String, ?]]", ParallelTests[Ior[String, ?], Ior[String, ?]].parallel[Int, String])
221+
checkAll("Parallel[IorT[F, String, ?], IorT[F, String, ?]] with parallel effect", ParallelTests[IorT[Either[String, ?], String, ?], IorT[Validated[String, ?], String, ?]].parallel[Int, String])
222+
checkAll("Parallel[IorT[F, String, ?], IorT[F, String, ?]] with sequential effect", ParallelTests[IorT[Option, String, ?], IorT[Option, String, ?]].parallel[Int, String])
174223
checkAll("Parallel[OptionT[M, ?], Nested[F, Option, ?]]", ParallelTests[OptionT[Either[String, ?], ?], Nested[Validated[String, ?], Option, ?]].parallel[Int, String])
175224
checkAll("Parallel[EitherT[M, String, ?], Nested[F, Validated[String, ?], ?]]", ParallelTests[EitherT[Either[String, ?], String, ?], Nested[Validated[String, ?], Validated[String, ?], ?]].parallel[Int, String])
176225
checkAll("Parallel[EitherT[Option, String, ?], Nested[Option, Validated[String, ?], ?]]", ParallelTests[EitherT[Option, String, ?], Nested[Option, Validated[String, ?], ?]].parallel[Int, String])

0 commit comments

Comments
 (0)