OxL Справочник OxL
РусскийEnglish

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:

oxl✓ проверяется в CI
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 BroadcastLocal fan-out hub
enum SlowPolicyWhat publish does when a subscriber has buffer unread elements.

Функции

batchGroups 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.
debounceEmits 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.
dedupDrops an element equal to the one before it.
filterThe elements for which p holds.
firstThe first element, or none for an empty stream.
mapApplies f to every element.
mergeBoth streams interleaved in arrival order (each is pumped by its own task).
scanRunning fold: emits the accumulator after every element.
skipEverything but the first n elements.
takeAt most the first n elements; the element after them is never requested from s.
throttleKeeps the first element of every interval of d, drops the rest.
windowTumbling time windows
zipPairs the n-th elements of both streams; ends when either ends.

См. также