This is an example when we want to execute 3 IO
in parallel
def test: Unit = {
val ioA = IO.shift *> IO(println("Running ioA"))
// ioA: cats.effect.IO[Unit] = <function1>
val ioB = IO.shift *> IO(println("Running ioB"))
// ioB: cats.effect.IO[Unit] = <function1>
val ioC = IO.shift *> IO(println("Running ioC"))
// ioC: cats.effect.IO[Unit] = <function1>
val program: IO[Unit] = (ioA, ioB, ioC).parMapN { (_, _, _) => () }
// program: cats.effect.IO[Unit] = <function1>
program.unsafeRunSync()
}
First question: What if the point of using IO.shift
in this example ?
Second question: What if we have a List
of IO
we want to execute in parallel ? I've created a function for this task but I don't know if this thing existed already in the library
def parallelize(ios: List[IO[Unit]]): IO[Unit] = {
ios.foldLeft(IO.pure(())) { case (currentResult, currentItem) =>
(currentResult, currentItem).parMapN((_, _) => ())
}
}
This works with "cats-core:1.1.0" and "cats-effect:0.10.1"
import cats.instances.list._
import cats.syntax.parallel._
//ios: List[IO[A]]
ios.parSequence //IO[List[A]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With