1 pointby gvsh_maths5 hours ago1 comment
  • gvsh_maths5 hours ago
    Iterflow is a zero-dependency TypeScript library that adds streaming statistical operations (rolling variance, EWMA, Pearson correlation, z-score anomaly detection) as chainable stages in lazy iterator pipelines.

    ES2025 Iterator Helpers give you lazy `map`, `filter`, `take` natively, but no statistics. @stdlib/stats/incr gives you streaming accumulators but they're standalone objects, not pipeline stages. If you want `filter -> window -> variance -> take` you're writing the loop yourself. Iterflow fills that gap:

    ```typescript import { iter } from '@mathscapes/iterflow';

    const anomalies = iter(requestLatencies) .streamingZScore() .enumerate() .filter(([_, z]) => Math.abs(z) > 3) .map(([i, z]) => ({ index: i, zScore: z })) .take(10) .toArray(); ```

    O(1) memory, stops after 10 anomalies, z-score is computed from prior observations only (current value doesn't skew its own score).

    Algorithms in there: Welford's variance, EWMA with configurable decay, streaming Pearson correlation (6 scalars of state), monotonic deque windowed min/max, circular buffer windowing, quickselect median. All implemented directly -- zero runtime deps.

    Some benchmark numbers (synthetic, Node.js v22, ARM64):

    - filter-map-take(1000) on 1M elements: ~296x over `.filter().map().slice()` - Streaming correlation vs recompute-each-step: ~780x at N=10k - Standalone single-stat with no pipeline: 3-5x slower than a hand-rolled loop (generator tax)

    I wrote a paper on the algorithms and benchmarks too: https://doi.org/10.5281/zenodo.18610143

    GitHub: https://github.com/mathscapes/iterflow npm: `npm install @mathscapes/iterflow`

    Curious if anyone has use cases I haven't considered, or thoughts on the API. MIT licensed.