module std.stream
std.stream — operators on streams and the Broadcast hub (§10.3, §10.4, II.1/II.2; D-2600..D-2619).
Every operator is an ordinary generator (yield, for await) over its input stream, so it is lazy: nothing runs
until the result is consumed, one element at a time (the producer of each stage is a task that suspends in yield
until it is asked for the next element, D-2601). Operators take the stream by value and own it: dropping the result
(or leaving a for await early) cancels every stage up to the source. Errors of the input stream pass through
unchanged and end the result. x |> f(args) is f(x, args) (§10.3).
Time based operators (batch, window, throttle, debounce) have the effect time.
Example:
use std.stream { map, filter, take }
fn nums() -> stream<i32> {
for i in 1..=100 { yield i }
}
fn main() io {
var out: List<i32> = []
for await x in nums() |> filter(x => *x % 2 == 0) |> map(x => x * 10) |> take(3) {
out.push(x)
}
print("{out}")
}Вывод
[20, 40, 60]
Типы
data Broadcast | Local fan-out hub |
enum SlowPolicy | What publish does when a subscriber has buffer unread elements. |
Функции
batch | Groups elements into lists of n; a list is also emitted when d has passed since its first element, and the last partial list when the stream ends. |
debounce | Emits an element only after d passed without a newer one (the last element of a burst); the pending element is emitted when the stream ends. |
dedup | Drops an element equal to the one before it. |
filter | The elements for which p holds. |
first | The first element, or none for an empty stream. |
map | Applies f to every element. |
merge | Both streams interleaved in arrival order (each is pumped by its own task). |
scan | Running fold: emits the accumulator after every element. |
skip | Everything but the first n elements. |
take | At most the first n elements; the element after them is never requested from s. |
throttle | Keeps the first element of every interval of d, drops the rest. |
window | Tumbling time windows |
zip | Pairs the n-th elements of both streams; ends when either ends. |