Created
October 14, 2019 08:56
-
-
Save ShahOdin/3131434bbe74171adc42cd0bcb0f21ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object demo extends IOApp { | |
implicit def catsDataMonadForNested[F[_]: Monad, G[_]: Monad: Traverse]: Monad[Nested[F, G, *]] = new StackSafeMonad[Nested[F, G, *]]{ | |
override def flatMap[A, B](fa: Nested[F, G, A])(f: A => Nested[F, G, B]): Nested[F, G, B] = Nested( | |
fa.value.flatMap{ | |
Traverse[G].flatTraverse(_)(f.andThen(_.value)) | |
} | |
) | |
override def pure[A](x: A): Nested[F, G, A] = cats.data.Nested.catsDataApplicativeForNested[F, G].pure(x) | |
} | |
implicit def parallelForNested[M[_], F[_], G[_]: Monad : Traverse](implicit p: Parallel[M, F]): Parallel[Nested[M, G, *], Nested[F, G, *]] = new Parallel[Nested[M, G, *], Nested[F, G, *]] { | |
implicit val F: Applicative[F] = p.applicative | |
implicit val M: Monad[M] = p.monad | |
override def applicative: Applicative[Nested[F, G, *]] = cats.data.Nested.catsDataApplicativeForNested[F, G] | |
override def monad: Monad[Nested[M, G, *]] = catsDataMonadForNested[M, G] | |
override def sequential: Nested[F, G, *] ~> Nested[M, G, *] = new ~>[Nested[F, G, *], Nested[M, G, *]] { | |
override def apply[A](fa: Nested[F, G, A]): Nested[M, G, A] = Nested(p.sequential.apply(fa.value)) | |
} | |
override def parallel: Nested[M, G, *] ~> Nested[F, G, *] = new ~>[Nested[M, G, *], Nested[F, G, *]] { | |
override def apply[A](fa: Nested[M, G, A]): Nested[F, G, A] = Nested(p.parallel.apply(fa.value)) | |
} | |
} | |
import cats.effect.IO | |
def test(implicit cs: ContextShift[IO]) = { | |
//implicit def ioParCommutativeApplicative(implicit cs: ContextShift[IO]): CommutativeApplicative[IO.Par] = ??? | |
val foo: IO[Option[Int]] = IO.pure(Some(1)) | |
val bar: IO[Option[Int]] = IO.pure(Some(2)) | |
val result2 = (OptionT(bar), OptionT(foo)).parMapN(_ + _).value | |
val result3 = (foo.nested, bar.nested).parMapN( _ + _)(parallelForNested).value | |
val fooList: IO[List[Int]] = IO.pure(List(1,3)) | |
val barList: IO[List[Int]] = IO.pure(List(2)) | |
val result4 = (fooList.nested, barList.nested).parMapN(_ + _)(parallelForNested).value | |
val result5 = (fooList.nested, barList.nested).mapN(_ + _).value | |
val lookup1: IO[Either[String, Option[Int]]] = IO.pure(1.some.asRight) | |
val lookup2: IO[Either[String, Option[Int]]] = IO.pure(2.some.asRight) | |
import com.itv.sif.db.KVLookup.fetchMonad | |
import cats.instances.option._ | |
import cats.instances.either._ | |
val x = { | |
(lookup1.nested, lookup2.nested).parMapN{ | |
case tuple => tuple.mapN(_ + _) | |
}.value | |
} | |
import Nested.catsDataTraverseForNested | |
val y = ( | |
lookup1.nested.nested, | |
lookup2.nested.nested | |
).parMapN(_ + _).value.value | |
y | |
} | |
import cats.effect._ | |
override def run(args: List[String]): IO[ExitCode] = test.map{ | |
i => | |
println(i) | |
ExitCode.Success | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment