Stream module provides a powerful abstraction for working with sequences of values that are produced over time. Streams are pull-based with backpressure, making them ideal for processing large datasets, event streams, and continuous data sources.
Overview
AStream<A, E, R> describes a program that can:
- Emit many values of type
A - Fail with an error of type
E - Require services of type
R
Effect, but adapted for multiple values.
Creating Streams
From Iterables
From Effects
From Async Sources
Paginated Data
Transforming Streams
Basic Transformations
Effectful Transformations
FlatMap and Chaining
Consuming Streams
Running to Collection
Running with Effects
Folding and Reducing
Error Handling
Merging and Combining
Merge Streams
Zip Streams
Concat Streams
Grouping and Chunking
Scheduling and Timing
Integration with PubSub
Best Practices
- Use Stream.runForEach for side effects: Process each element with effects
- Leverage chunking: Use
groupedorgroupedWithinfor batch processing - Control concurrency: Use
mapEffectParwith appropriate concurrency limits - Handle backpressure: Streams naturally handle backpressure with pull-based semantics
- Compose streams: Build complex streams from simpler ones
Performance Tips
- Use
Stream.bufferto improve throughput - Process in chunks when possible to amortize effect costs
- Use
mapEffectParfor concurrent processing of independent elements - Consider
Stream.throttleto control emission rate