Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Stream[IO, List[A]] to Stream[IO, A]

I want to parse a json file which output a collection of A. The signature of the Output is IO[List[A]]

How can I convert this value to a Stream: Stream[IO, A] ? I can convert to a Stream[IO, List[A]] but it is not what I want

fs2.Stream.eval(input).flatMap(x => fs2.Stream.apply(x)) Thanks

like image 974
nam Avatar asked Dec 08 '22 15:12

nam


2 Answers

You can also use Stream.emits, which accepts a Seq, so fs2.Stream.eval(output).flatMap(fs2.Stream.emits(_)).

This is more efficient than using varargs with apply because it avoids wrapping and unwrapping the sequence structure - this can save a lot in the case of specialized primitive collections.

like image 123
Daenyth Avatar answered Dec 29 '22 11:12

Daenyth


Try

fs2.Stream.eval(output).flatMap(x => fs2.Stream.apply(x: _*))

What does `:_*` (colon underscore star) do in Scala?

like image 23
Dmytro Mitin Avatar answered Dec 29 '22 13:12

Dmytro Mitin