Skip to content

Commit

Permalink
Move appError instance to Parallel companion object
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Aug 29, 2017
1 parent e72b087 commit ab229b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
16 changes: 16 additions & 0 deletions core/src/main/scala/cats/Parallel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ object Parallel extends ParallelArityFunctions {
(ma: M[A], mb: M[B])
(implicit P: Parallel[M, F]): M[Z] =
Monad[M].map(parProduct(ma, parProduct(mb, ff))) { case (a, (b, f)) => f(a, b) }

def applicativeError[F[_], M[_], E]
(implicit P: Parallel[M, F], E: MonadError[M, E]): ApplicativeError[F, E] = new ApplicativeError[F, E] {

def raiseError[A](e: E): F[A] =
P.parallel.apply(MonadError[M, E].raiseError(e))

def handleErrorWith[A](fa: F[A])(f: (E) => F[A]): F[A] = {
val ma = MonadError[M, E].handleErrorWith(P.sequential.apply(fa))(f andThen P.sequential.apply)
P.parallel.apply(ma)
}

def pure[A](x: A): F[A] = P.applicative.pure(x)

def ap[A, B](ff: F[(A) => B])(fa: F[A]): F[B] = P.applicative.ap(ff)(fa)
}
}
18 changes: 1 addition & 17 deletions core/src/main/scala/cats/instances/parallel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cats.instances
import cats.data.{EitherT, Nested, OptionT, Validated}
import cats.kernel.Semigroup
import cats.syntax.either._
import cats.{Applicative, ApplicativeError, Functor, Monad, MonadError, Parallel, ~>}
import cats.{Applicative, Functor, Monad, Parallel, ~>}


trait ParallelInstances {
Expand All @@ -17,22 +17,6 @@ trait ParallelInstances {
λ[Either[E, ?] ~> Validated[E, ?]](_.toValidated)
}

implicit def catsApplicativeErrorForParallelMonadError[F[_], M[_], E]
(implicit P: Parallel[M, F], E: MonadError[M, E]): ApplicativeError[F, E] = new ApplicativeError[F, E] {

def raiseError[A](e: E): F[A] =
P.parallel.apply(MonadError[M, E].raiseError(e))

def handleErrorWith[A](fa: F[A])(f: (E) => F[A]): F[A] = {
val ma = MonadError[M, E].handleErrorWith(P.sequential.apply(fa))(f andThen P.sequential.apply)
P.parallel.apply(ma)
}

def pure[A](x: A): F[A] = P.applicative.pure(x)

def ap[A, B](ff: F[(A) => B])(fa: F[A]): F[B] = P.applicative.ap(ff)(fa)
}

implicit def catsParallelForOptionTNestedOption[F[_], M[_]: Monad]
(implicit P: Parallel[M, F]): Parallel[OptionT[M, ?], Nested[F, Option, ?]] = new Parallel[OptionT[M, ?], Nested[F, Option, ?]] {
implicit val appF: Applicative[F] = P.applicative
Expand Down

0 comments on commit ab229b9

Please sign in to comment.