Stream[+E, +A] is a lazy, pull-based, typed-error stream of elements. It is purely functional, referentially transparent, and resource-safe.
Key characteristics:
- Lazy: It describes a pipeline; no computation occurs until a terminal operation is called.
- Synchronous: Terminal operations return
Either[E, Z] directly. - Pull-based: The sink (consumer) drives execution backward through the pipeline, enabling automatic short-circuiting.
- Typed Errors: Distinguishes recoverable domain errors (
E) from untyped defects (Throwable). - Resource-safe: Uses RAII semantics to ensure resources are released regardless of success, error, or cancellation.
abstract class Stream[+E, +A] {
def run[E2 >: E, Z](sink: Sink[E2, A, Z]): Either[E2, Z]
def runCollect: Either[E, Chunk[A]]
}