Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

windowed

Sliding windows of size consecutive elements, each starting step elements after the last.

Iterable<List<A>> windowed<A>(int size, Iterable<A> iterable, {int step = 1, bool partial = false}) FxAsyncIterable<List<A>> windowedAsync<A>(int size, FxAsyncIterable<A> iterable, {int step = 1, bool partial = false}) Fx<List<T>> Fx<T>.windowed(int size, {int step = 1, bool partial = false}) // chain (sync) FxAsync<List<T>> FxAsync<T>.windowed(int size, {int step = 1, bool partial = false}) // chain (async)

Lecture

chunk cuts a sequence into non-overlapping pieces. The moment the pieces must overlap — a moving average, "three consecutive readings over the limit", any each-element-with-its-neighbors question — you end up hand-rolling an index loop with careful bounds. windowed(size) is that loop as a lazy operator: it yields a List of each size consecutive elements, sliding forward by step (default 1) between windows.

Two knobs cover the whole family. step spaces the windows: step < size overlaps them, step == size tiles them exactly like chunk, and step > size samples with gaps. partial: true keeps the shorter windows at the tail instead of dropping them — in fact chunk(n) is windowed(n, step: n, partial: true); they share one implementation.

fxdart extension (no FxTS counterpart) — named after Kotlin's windowed; RxDart readers know it as bufferCount(size, startEvery). It is lazy like every fxdart operator: windows materialize one pull at a time, so it composes with endless sources and with concurrent.

Demo 1 · Moving average

Demo 2 · The step & partial knobs

Try it yourself

Exercise: flag three consecutive days over the spending limit.

Related: chunk — the non-overlapping special case · pairwise — windows of exactly two, as records · scan — running state without a fixed window