Skip to content

Commit

Permalink
Add parallel instance for IorT
Browse files Browse the repository at this point in the history
  • Loading branch information
andyscott committed Dec 4, 2017
1 parent c51fe61 commit 4ee93ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/data/IorT.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package data

import cats.arrow.FunctionK
import cats.syntax.either._
import cats.syntax.option._

Expand Down Expand Up @@ -426,6 +427,27 @@ private[data] abstract class IorTInstances1 extends IorTInstances2 {
val A0: Semigroup[A] = A
val F0: Monad[F] = F
}

implicit def parallelForIorT[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, ?]]

def parallel: IorT[F, E, ?] ~> IorT[F, E, ?] = identityK
def sequential: IorT[F, E, ?] ~> IorT[F, E, ?] = identityK

val applicative: Applicative[IorT[F, E, ?]] = new Applicative[IorT[F, E, ?]] {
def pure[A](a: A): IorT[F, E, A] = IorT.pure(a)
def ap[A, B](ff: IorT[F, E, A => B])(fa: IorT[F, E, A]): IorT[F, E, B] =
IorT(F.map2(ff.value, fa.value)((f, a) => underlyingParallel.applicative.ap(f)(a)))
}

lazy val monad: Monad[IorT[F, E, ?]] = Monad[IorT[F, E, ?]]
}

}

private[data] abstract class IorTInstances2 extends IorTInstances3 {
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/ParallelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest {

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[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])
Expand Down

0 comments on commit 4ee93ae

Please sign in to comment.