0

I have an FS2 stream that reads a queue and for each received message, it then reads a file as a stream. How do I model such a stream inside a stream in FS2 ?

I can achieve this using a flatMap operation such as Stream(1,2,3).flatMap(i => Stream(i,i)).toList, but is there any FS2 style way to do this?

At the end of the second inner stream, I also want to transform the original first outer stream with a FS2.through using a pipe.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Jeet Banerjee
  • 194
  • 2
  • 2
  • 12
  • 2
    Uhm what is wrong with `flatMap`? Note that `flatMap` would be sequential which may or may not be what you want, other than that it is the **fs2** way of doing things. – Luis Miguel Mejía Suárez Mar 23 '23 at 16:11
  • @LuisMiguelMejíaSuárez If I use `flatMap`, how can I use a `Stream.through` at the end on the first outer stream? – Jeet Banerjee Mar 23 '23 at 16:16
  • So, you have some input `A` which comes from the `Queue`, then for each `A` you will read some other input `B` which comes from some **files**. Then you want to apply a `through` that produces some output `C`. _Now, what it is not clear to me is how that `pipe` interacts with the two other inputs, do you want to apply it to all the `Bs` of all the `As`? Or, one different `pipe` for each `B` of each `A`? Or for the original `As`? If that last one, how would the `Cs` interact with the `Bs`? – Luis Miguel Mejía Suárez Mar 23 '23 at 16:22
  • I want to apply pipe only to the input of `As`, you can think of it like a "delete message from the queue" which happens as a last step. The `Bs` have nothing to do with pipe. – Jeet Banerjee Mar 23 '23 at 16:26
  • I assume such `pipe` is equvalent to a `a => Stream.exec(f(a))` where `f` is the operation to apply. And, you want to do that after the respective file was successfully processed, in such case you can do this. `inputs.flatMap(a => processFile(a) ++ Stream.exec(f(a)))` – Luis Miguel Mejía Suárez Mar 23 '23 at 16:33
  • Thank you @LuisMiguelMejíaSuárez, will give it a try and let you know how it goes. – Jeet Banerjee Mar 23 '23 at 17:06
  • @LuisMiguelMejíaSuárez I don't really want to concat the two streams so using `++ Stream.exec(f(a))` will not work. – Jeet Banerjee Mar 24 '23 at 11:46
  • Well, again, why you don't want to concatenate? Note that I expect `f(a)` to be `F[Unit]` thus `Stream.exec` will create a `Stream[Nothin]` that produces no output at all and that will simply execute the effect on the file is consumed. But, this makes sense because `f(a)` is for this single `a` not the whole `pipe` - BTW, I may recommend moving the question to the **Discord** server, it may be easier to help in chat: https://discord.com/channels/632277896739946517/632310980449402880 – Luis Miguel Mejía Suárez Mar 24 '23 at 13:26

0 Answers0