Introduction to Sinks
In the world of streams, a Sink<A, In, L, E, R>
plays a crucial role. It's like a specialized function designed to consume elements generated by a Stream
. Here's a breakdown of what a Sink
does:
- It can consume a varying number of
In
elements, which might be zero, one, or more. - It has the potential to encounter errors of type
E
. - Ultimately, it produces a value of type
A
. - Additionally, it can return a remainder of type
L
, which represents any leftover elements.
To use a Sink
for processing a stream, you simply pass it to the Stream.run
function:
ts
import {Stream ,Sink ,Effect } from "effect"conststream =Stream .make (1, 2, 3) // Define a stream with numbers 1, 2, and 3constsink =Sink .sum // Choose a sink that sums up numbersconstsum =Stream .run (stream ,sink ) // Run the stream through the sinkEffect .runPromise (sum ).then (console .log )/*Output:6*/
ts
import {Stream ,Sink ,Effect } from "effect"conststream =Stream .make (1, 2, 3) // Define a stream with numbers 1, 2, and 3constsink =Sink .sum // Choose a sink that sums up numbersconstsum =Stream .run (stream ,sink ) // Run the stream through the sinkEffect .runPromise (sum ).then (console .log )/*Output:6*/