consume
Pulls values through a chain and throws them away — for side effects only, optionally capped.
Lecture
consume is the minimal terminal operator: it pulls values
through the chain — running whatever peek or
mapEffect steps are upstream for their side effects — but
discards every value instead of collecting or forwarding it. Reach for
it when the whole point of a pipeline is its side effects, and building
a List with toList would just be wasted
allocation.
The optional n makes it the natural partner for infinite
or huge sources: consume(5) pulls exactly 5 values and
stops, even if the underlying iterable (cycle, which never
ends, or an fxRange or repeat with a huge
count) would otherwise run for a very long time. Omit n to drain a finite iterable
completely.
fxConsumeAsync (or .consume() on an
FxAsync chain) works the same way, awaiting each pulled
value's side effects in turn — handy for forcing an async
peek/logging pipeline to actually run without paying to
collect a result list you'd throw away anyway.
Demo 1 · Bounding a huge source
fxRange(1000000) is finite, but fully pulling it would visit
a million values — consume(5) stops after 5 elements:
Demo 2 · Async side effects, no result list
Try it yourself
Exercise: consume only the first 3 values of a huge
repeat(), logging each one as it's pulled.