Home > Net >  Difference between parSequence and starting fibers manually
Difference between parSequence and starting fibers manually

Time:12-25

I have got two program's implementatnion

def program_valid: IO[Unit] = for {
    interpreter <- Interpreter[IO]
    fib1 <- display(interpreter).start
    fib2 <- read(interpreter).start
    _ <- fib1.join
    _ <- fib2.join
  } yield ()

  def program_invalid: IO[Unit] = for {
    interpreter <- Interpreter[IO]
    _ <- (read(interpreter), display(interpreter)).parSequence
  } yield ()

First one works perfectly well which means both fibers keep running (dispaly and read). Unfortunately second implementatnion works different. It looks like only the display fiber would run. Why it goes like that? What is the difference here?

CodePudding user response:

parSequence is for collections (well actually for types with an instance of Traverse), not sure how it even compiles; well I mean there is probably a Traverse instance for Tuple2 but it definitively doesn't do what you want.

You may use:

_ <- (read(interpreter), display(interpreter)).parTupled.void
// Or
_ <- IO.both(read(interpreter), display(interpreter)).void
// Or
_ <- read(interpreter).both(display(interpreter)).void
// Or
_ <- List(read(interpreter), display(interpreter)).parSequence_
  • Related