Leftovers
When a Stream delivers a batch (chunk) to a Sink and the sink finishes before exhausting that batch, the unused suffix of that batch becomes leftovers. Leftovers let composed or repeated sinks (Stream.transduce) resume without dropping already-pulled elements—they are not the entire unread remainder of the upstream stream.
Collecting leftover elements
effect function inspectRemainder() {
const source = Stream.fromArray([21, 34, 55, 89])
const sink = Sink.mapEnd(Sink.take<number>(2), ([selected, leftover]) => [
{ selected, leftover: leftover ?? [] }
] as const)
return try Stream.run(source, sink)
}
// { selected: [21, 34], leftover: [55, 89] }
// Run in an effect function:
// {
// const jsonValue = try inspectRemainder()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"selected":[21,34],"leftover":[55,89]}Sink.mapEnd intercepts [result, optionalLeftoverBatch] as the sink finishes and returns a replacement tuple. Returning a 1-tuple folds the leftovers into the sink's success value A and clears the separate leftover slot, so try Stream.run returns both together.
effect function inspectSmallBatches() {
const source = Stream.rechunk(Stream.fromArray([21, 34, 55, 89]), 2)
const sink = Sink.mapEnd(Sink.head<number>(), ([first, leftover]) => [
{ first, leftover: leftover ?? [] }
] as const)
return try Stream.run(source, sink)
}
// { first: Option.some(21), leftover: [34] }
// Run in an effect function:
// const remainder = try inspectSmallBatches()
// {
// const jsonValue = { first: Option.getOrElse(remainder.first, () => 0), leftover: remainder.leftover }
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"first":21,"leftover":[34]}Because rechunk(..., 2) splits the stream into [21, 34] and [55, 89], Sink.head() pulls only the first batch—yielding 21 and leftover [34]. The second batch (55, 89) is never requested and therefore never appears in leftovers. When you need both a prefix result and a live stream of every remaining element (requested or not), use Stream.peel inside a Scope.
| Need | API | Sees unpulled upstream batches? |
|---|---|---|
| Inspect unused suffix of current batch | Sink.mapEnd | No |
| Run a prefix sink, then stream the entire tail | Stream.peel (scoped) | Yes |
Drop leftovers and set L = never | Sink.ignoreLeftover | No |
Explicitly discarding leftovers
effect function discardRemainder() {
const sink = Sink.take<number>(2).pipe(
Sink.ignoreLeftover,
Sink.mapEnd(([selected, leftover]) => [
{ selected, leftover: leftover ?? [] }
] as const)
)
return try Stream.run(Stream.fromArray([21, 34, 55, 89]), sink)
}
// { selected: [21, 34], leftover: [] }
// Run in an effect function:
// {
// const jsonValue = try discardRemainder()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"selected":[21,34],"leftover":[]}Sink.ignoreLeftover drops the unused batch suffix and narrows L to never without draining upstream. Use it only when discarding already-pulled elements is intentional—in composed sinks or Stream.transduce, dropping leftovers silently skips elements between runs.
The matching Effect page: effect.website/docs/v4/sink/leftovers