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

Make Parallel's F a type member #3012

Merged
merged 2 commits into from
Aug 26, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import cats.syntax.either._
import cats.{~>, Applicative, Apply, FlatMap, Functor, Monad, NonEmptyParallel, Parallel}

trait ParallelInstances extends ParallelInstances1 {
implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel[Either[E, *], Validated[E, *]] =
new Parallel[Either[E, *], Validated[E, *]] {
implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel.Aux[Either[E, *], Validated[E, *]] =
new Parallel[Either[E, *]] {
type F[x] = Validated[E, x]

def applicative: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
def monad: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither
Expand All @@ -20,27 +21,28 @@ trait ParallelInstances extends ParallelInstances1 {
λ[Either[E, *] ~> Validated[E, *]](_.toValidated)
}

implicit def catsParallelForOptionTNestedOption[F[_], M[_]](
implicit P: Parallel[M, F]
): Parallel[OptionT[M, *], Nested[F, Option, *]] = new Parallel[OptionT[M, *], Nested[F, Option, *]] {
implicit def catsParallelForOptionTNestedOption[M[_]](
implicit P: Parallel[M]
): Parallel.Aux[OptionT[M, *], Nested[P.F, Option, *]] = new Parallel[OptionT[M, *]] {
type F[x] = Nested[P.F, Option, x]

implicit val appF: Applicative[F] = P.applicative
implicit val monadM: Monad[M] = P.monad
implicit val appOption: Applicative[Option] = cats.instances.option.catsStdInstancesForOption

def applicative: Applicative[Nested[F, Option, *]] = cats.data.Nested.catsDataApplicativeForNested[F, Option]
def applicative: Applicative[Nested[P.F, Option, *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, cats.instances.option.catsStdInstancesForOption)

def monad: Monad[OptionT[M, *]] = cats.data.OptionT.catsDataMonadErrorMonadForOptionT[M]

def sequential: Nested[F, Option, *] ~> OptionT[M, *] =
λ[Nested[F, Option, *] ~> OptionT[M, *]](nested => OptionT(P.sequential(nested.value)))
def sequential: Nested[P.F, Option, *] ~> OptionT[M, *] =
λ[Nested[P.F, Option, *] ~> OptionT[M, *]](nested => OptionT(P.sequential(nested.value)))

def parallel: OptionT[M, *] ~> Nested[F, Option, *] =
λ[OptionT[M, *] ~> Nested[F, Option, *]](optT => Nested(P.parallel(optT.value)))
def parallel: OptionT[M, *] ~> Nested[P.F, Option, *] =
λ[OptionT[M, *] ~> Nested[P.F, Option, *]](optT => Nested(P.parallel(optT.value)))
}

implicit def catsStdNonEmptyParallelForZipList[A]: NonEmptyParallel[List, ZipList] =
new NonEmptyParallel[List, ZipList] {
implicit def catsStdNonEmptyParallelForZipList[A]: NonEmptyParallel.Aux[List, ZipList] =
new NonEmptyParallel[List] {
type F[x] = ZipList[x]

def flatMap: FlatMap[List] = cats.instances.list.catsStdInstancesForList
def apply: Apply[ZipList] = ZipList.catsDataCommutativeApplyForZipList
Expand All @@ -52,8 +54,9 @@ trait ParallelInstances extends ParallelInstances1 {
λ[List ~> ZipList](v => new ZipList(v))
}

implicit def catsStdNonEmptyParallelForZipVector[A]: NonEmptyParallel[Vector, ZipVector] =
new NonEmptyParallel[Vector, ZipVector] {
implicit def catsStdNonEmptyParallelForZipVector[A]: NonEmptyParallel.Aux[Vector, ZipVector] =
new NonEmptyParallel[Vector] {
type F[x] = ZipVector[x]

def flatMap: FlatMap[Vector] = cats.instances.vector.catsStdInstancesForVector
def apply: Apply[ZipVector] = ZipVector.catsDataCommutativeApplyForZipVector
Expand All @@ -65,8 +68,9 @@ trait ParallelInstances extends ParallelInstances1 {
λ[Vector ~> ZipVector](v => new ZipVector(v))
}

implicit def catsStdParallelForZipStream[A]: Parallel[Stream, ZipStream] =
new Parallel[Stream, ZipStream] {
implicit def catsStdParallelForZipStream[A]: Parallel.Aux[Stream, ZipStream] =
new Parallel[Stream] {
type F[x] = ZipStream[x]

def monad: Monad[Stream] = cats.instances.stream.catsStdInstancesForStream
def applicative: Applicative[ZipStream] = ZipStream.catsDataAlternativeForZipStream
Expand All @@ -78,31 +82,30 @@ trait ParallelInstances extends ParallelInstances1 {
λ[Stream ~> ZipStream](v => new ZipStream(v))
}

implicit def catsParallelForEitherTNestedParallelValidated[F[_], M[_], E: Semigroup](
implicit P: Parallel[M, F]
): Parallel[EitherT[M, E, *], Nested[F, Validated[E, *], *]] =
new Parallel[EitherT[M, E, *], Nested[F, Validated[E, *], *]] {
implicit def catsParallelForEitherTNestedParallelValidated[M[_], E: Semigroup](
implicit P: Parallel[M]
): Parallel.Aux[EitherT[M, E, *], Nested[P.F, Validated[E, *], *]] =
new Parallel[EitherT[M, E, *]] {
type F[x] = Nested[P.F, Validated[E, *], x]

implicit val appF: Applicative[F] = P.applicative
implicit val monadM: Monad[M] = P.monad
implicit val appValidated: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[F, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested[F, Validated[E, *]]
def applicative: Applicative[Nested[P.F, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, Validated.catsDataApplicativeErrorForValidated)

def monad: Monad[EitherT[M, E, *]] = cats.data.EitherT.catsDataMonadErrorForEitherT

def sequential: Nested[F, Validated[E, *], *] ~> EitherT[M, E, *] =
λ[Nested[F, Validated[E, *], *] ~> EitherT[M, E, *]] { nested =>
def sequential: Nested[P.F, Validated[E, *], *] ~> EitherT[M, E, *] =
λ[Nested[P.F, Validated[E, *], *] ~> EitherT[M, E, *]] { nested =>
val mva = P.sequential(nested.value)
EitherT(Functor[M].map(mva)(_.toEither))
}

def parallel: EitherT[M, E, *] ~> Nested[F, Validated[E, *], *] =
λ[EitherT[M, E, *] ~> Nested[F, Validated[E, *], *]] { eitherT =>
def parallel: EitherT[M, E, *] ~> Nested[P.F, Validated[E, *], *] =
λ[EitherT[M, E, *] ~> Nested[P.F, Validated[E, *], *]] { eitherT =>
val fea = P.parallel(eitherT.value)
Nested(Functor[F].map(fea)(_.toValidated))
Nested(P.applicative.map(fea)(_.toValidated))
}
}
}
5 changes: 3 additions & 2 deletions core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ sealed abstract private[data] class NonEmptyLazyListInstances extends NonEmptyLa
implicit def catsDataShowForNonEmptyLazyList[A](implicit A: Show[A]): Show[NonEmptyLazyList[A]] =
Show.show[NonEmptyLazyList[A]](nell => s"NonEmpty${Show[LazyList[A]].show(nell.toLazyList)}")

implicit def catsDataParallelForNonEmptyLazyList: Parallel[NonEmptyLazyList, OneAnd[ZipLazyList, *]] =
new Parallel[NonEmptyLazyList, OneAnd[ZipLazyList, *]] {
implicit def catsDataParallelForNonEmptyLazyList: Parallel.Aux[NonEmptyLazyList, OneAnd[ZipLazyList, *]] =
new Parallel[NonEmptyLazyList] {
type F[x] = OneAnd[ZipLazyList, x]

def applicative: Applicative[OneAnd[ZipLazyList, *]] =
OneAnd.catsDataApplicativeForOneAnd(ZipLazyList.catsDataAlternativeForZipLazyList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import cats.syntax.either._
import cats.{~>, Applicative, Apply, FlatMap, Functor, Monad, NonEmptyParallel, Parallel}

trait ParallelInstances extends ParallelInstances1 {
implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel[Either[E, *], Validated[E, *]] =
new Parallel[Either[E, *], Validated[E, *]] {
implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel.Aux[Either[E, *], Validated[E, *]] =
new Parallel[Either[E, *]] {
type F[x] = Validated[E, x]

def applicative: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
def monad: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither
Expand All @@ -20,27 +21,28 @@ trait ParallelInstances extends ParallelInstances1 {
λ[Either[E, *] ~> Validated[E, *]](_.toValidated)
}

implicit def catsParallelForOptionTNestedOption[F[_], M[_]](
implicit P: Parallel[M, F]
): Parallel[OptionT[M, *], Nested[F, Option, *]] = new Parallel[OptionT[M, *], Nested[F, Option, *]] {
implicit def catsParallelForOptionTNestedOption[M[_]](
implicit P: Parallel[M]
): Parallel.Aux[OptionT[M, *], Nested[P.F, Option, *]] = new Parallel[OptionT[M, *]] {
type F[x] = Nested[P.F, Option, x]

implicit val appF: Applicative[F] = P.applicative
implicit val monadM: Monad[M] = P.monad
implicit val appOption: Applicative[Option] = cats.instances.option.catsStdInstancesForOption

def applicative: Applicative[Nested[F, Option, *]] = cats.data.Nested.catsDataApplicativeForNested[F, Option]
def applicative: Applicative[Nested[P.F, Option, *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, cats.instances.option.catsStdInstancesForOption)

def monad: Monad[OptionT[M, *]] = cats.data.OptionT.catsDataMonadErrorMonadForOptionT[M]

def sequential: Nested[F, Option, *] ~> OptionT[M, *] =
λ[Nested[F, Option, *] ~> OptionT[M, *]](nested => OptionT(P.sequential(nested.value)))
def sequential: Nested[P.F, Option, *] ~> OptionT[M, *] =
λ[Nested[P.F, Option, *] ~> OptionT[M, *]](nested => OptionT(P.sequential(nested.value)))

def parallel: OptionT[M, *] ~> Nested[F, Option, *] =
λ[OptionT[M, *] ~> Nested[F, Option, *]](optT => Nested(P.parallel(optT.value)))
def parallel: OptionT[M, *] ~> Nested[P.F, Option, *] =
λ[OptionT[M, *] ~> Nested[P.F, Option, *]](optT => Nested(P.parallel(optT.value)))
}

implicit def catsStdNonEmptyParallelForZipList[A]: NonEmptyParallel[List, ZipList] =
new NonEmptyParallel[List, ZipList] {
implicit def catsStdNonEmptyParallelForZipList[A]: NonEmptyParallel.Aux[List, ZipList] =
new NonEmptyParallel[List] {
type F[x] = ZipList[x]

def flatMap: FlatMap[List] = cats.instances.list.catsStdInstancesForList
def apply: Apply[ZipList] = ZipList.catsDataCommutativeApplyForZipList
Expand All @@ -52,8 +54,9 @@ trait ParallelInstances extends ParallelInstances1 {
λ[List ~> ZipList](v => new ZipList(v))
}

implicit def catsStdNonEmptyParallelForZipVector[A]: NonEmptyParallel[Vector, ZipVector] =
new NonEmptyParallel[Vector, ZipVector] {
implicit def catsStdNonEmptyParallelForZipVector[A]: NonEmptyParallel.Aux[Vector, ZipVector] =
new NonEmptyParallel[Vector] {
type F[x] = ZipVector[x]

def flatMap: FlatMap[Vector] = cats.instances.vector.catsStdInstancesForVector
def apply: Apply[ZipVector] = ZipVector.catsDataCommutativeApplyForZipVector
Expand All @@ -66,8 +69,9 @@ trait ParallelInstances extends ParallelInstances1 {
}

@deprecated("Use catsStdParallelForZipLazyList", "2.0.0-RC2")
implicit def catsStdParallelForZipStream[A]: Parallel[Stream, ZipStream] =
new Parallel[Stream, ZipStream] {
implicit def catsStdParallelForZipStream[A]: Parallel.Aux[Stream, ZipStream] =
new Parallel[Stream] {
type F[x] = ZipStream[x]

def monad: Monad[Stream] = cats.instances.stream.catsStdInstancesForStream
def applicative: Applicative[ZipStream] = ZipStream.catsDataAlternativeForZipStream
Expand All @@ -79,8 +83,9 @@ trait ParallelInstances extends ParallelInstances1 {
λ[Stream ~> ZipStream](v => new ZipStream(v))
}

implicit def catsStdParallelForZipLazyList[A]: Parallel[LazyList, ZipLazyList] =
new Parallel[LazyList, ZipLazyList] {
implicit def catsStdParallelForZipLazyList[A]: Parallel.Aux[LazyList, ZipLazyList] =
new Parallel[LazyList] {
type F[x] = ZipLazyList[x]

def monad: Monad[LazyList] = cats.instances.lazyList.catsStdInstancesForLazyList
def applicative: Applicative[ZipLazyList] = ZipLazyList.catsDataAlternativeForZipLazyList
Expand All @@ -92,31 +97,30 @@ trait ParallelInstances extends ParallelInstances1 {
λ[LazyList ~> ZipLazyList](v => new ZipLazyList(v))
}

implicit def catsParallelForEitherTNestedParallelValidated[F[_], M[_], E: Semigroup](
implicit P: Parallel[M, F]
): Parallel[EitherT[M, E, *], Nested[F, Validated[E, *], *]] =
new Parallel[EitherT[M, E, *], Nested[F, Validated[E, *], *]] {
implicit def catsParallelForEitherTNestedParallelValidated[M[_], E: Semigroup](
implicit P: Parallel[M]
): Parallel.Aux[EitherT[M, E, *], Nested[P.F, Validated[E, *], *]] =
new Parallel[EitherT[M, E, *]] {
type F[x] = Nested[P.F, Validated[E, *], x]

implicit val appF: Applicative[F] = P.applicative
implicit val monadM: Monad[M] = P.monad
implicit val appValidated: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[F, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested[F, Validated[E, *]]
def applicative: Applicative[Nested[P.F, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, Validated.catsDataApplicativeErrorForValidated)

def monad: Monad[EitherT[M, E, *]] = cats.data.EitherT.catsDataMonadErrorForEitherT

def sequential: Nested[F, Validated[E, *], *] ~> EitherT[M, E, *] =
λ[Nested[F, Validated[E, *], *] ~> EitherT[M, E, *]] { nested =>
def sequential: Nested[P.F, Validated[E, *], *] ~> EitherT[M, E, *] =
λ[Nested[P.F, Validated[E, *], *] ~> EitherT[M, E, *]] { nested =>
val mva = P.sequential(nested.value)
EitherT(Functor[M].map(mva)(_.toEither))
}

def parallel: EitherT[M, E, *] ~> Nested[F, Validated[E, *], *] =
λ[EitherT[M, E, *] ~> Nested[F, Validated[E, *], *]] { eitherT =>
def parallel: EitherT[M, E, *] ~> Nested[P.F, Validated[E, *], *] =
λ[EitherT[M, E, *] ~> Nested[P.F, Validated[E, *], *]] { eitherT =>
val fea = P.parallel(eitherT.value)
Nested(Functor[F].map(fea)(_.toValidated))
Nested(P.applicative.map(fea)(_.toValidated))
}
}
}
Loading