Search code examples
scalaziozio-streams

ZIO Streams: Which is the difference between a ZSink and a ZTransducer?


I'm studying ZIO Streams, using version 1.0.9 of the library zio-streams. I cannot find any reference that shows me the difference between a ZSink and a ZTransducer.

What is the difference?


Solution

  • A ZSink is a used to consume elements from the ZStream. In turn a sink will cause the materialization of the entire stream to run through and produce a value.

    A ZTransducer is a stateful function which processes chunks of data in the stream, and in contrast to a ZSink it's processing never ends.

    If we look at a trivial example:

    import zio.stream.{ ZSink, ZStream }
    import zio.stream.ZTransducer.{ splitLines, utf8Decode }
    
    import java.nio.file.Paths
    
    ZStream
      .fromFile(Paths.get("/foo"), chunkSize = 1024 * 4)
      .transduce(utf8Decode >>> splitLines)
      .run(ZSink.foreach(zio.console.putStr))
    

    We can see that the new ZStream encoding is chunk based, and using transduce with utf8Decode which decodes a chunk of bytes to String and then splits them by line. Finally, we use the .run method and supply a ZSink which emits data to the console, element by element.

    More on the new design of ZStream can be found here.