Skip to main content
Version: 2.x

Introduction to ZSink

A ZSink[R, E, I, L, Z] is used to consume elements produced by a ZStream. You can think of a sink as a function that will consume a variable amount of I elements (could be 0, 1, or many!), might fail with an error of type E, and will eventually yield a value of type Z together with a remainder of type L as leftover.

To consume a stream using ZSink we can pass ZSink to the ZStream#run function:

import zio._
import zio.stream._

val stream = ZStream.fromIterable(1 to 1000)
val sink = ZSink.sum[Int]
val sum = stream.run(sink)

ZSink has one type alias called Sink. Sink[E, A, L, B] is a type alias for ZSink[Any, E, A, L, B]. We can think of a Sink as a function that does not require any services and will consume a variable amount of A elements (could be 0, 1, or many!), might fail with an error of type E, and will eventually yield a value of type B. The L is the type of elements in the leftover.

type Sink[+E, A, +L, +B] = ZSink[Any, E, A, L, B]

See Alsoโ€‹

  • ZSink โ€” Describes concurrent and parallel operations with multiple sinks.
  • Creating Sinks โ€” Explains how to create ZSink instances
  • Sink Operations โ€” Describes how to transform and operate on sinks

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–