I have a List[IO[Unit]]
which consists of distinct works encoded in IO
. I would like to convert it into Stream[IO,Unit]
Currently, what I have done is this
val works: List[IO[Unit]]
works.map(work => Stream.eval(work)).reduceLeft((left, right) => left ++ right)
Is there a better way?
I suggest doing one of those:
Stream.emits(works).flatMap(Stream.eval) // or
Stream.emits(works).evalMap(x => x) // which is the same, or
Stream(works: _*).evalMap(x => x) // which is also the same
There's a difference vs. doing Stream.evals
on a works.sequence
in that you lose the fact that your effect is made of different parts. That will surface itself if you run stream partially, e.g. by doing:
stream.take(3).compile.drain
If you do not sequence, it will only run first 3 elements from works
. If you do, it will run all of them.
From other options proposed by Luis Miguel, only the unfoldEval
one preserves the separation, but it's probably an overkill.