Streams — Mapanare

Asynchronous pipelines with the |> pipe operator and automatic stream fusion.

Overview

Streams are asynchronous iterables that produce values over time. The |> pipe operator chains transformations, and the compiler automatically fuses adjacent operations into a single pass.

Basic Usage

let result = stream([1, 2, 3, 4, 5])
    |> filter(fn(x) { x > 2 })
    |> map(fn(x) { x * 10 })
    |> fold(0, fn(acc, x) { acc + x })

Stream Operators

map(fn), filter(fn), flat_map(fn), take(n), skip(n), chunk(n), zip(other), merge(other), fold(init, fn), scan(init, fn), distinct(), throttle(ms), debounce(ms).

Hot vs Cold Streams

Cold: Values produced on demand, each subscriber gets independent sequence. Hot: Values produced continuously, subscribers see values from subscription point.

Backpressure

Strategies: buffer(n), drop_oldest, drop_newest, error.

Stream Fusion

The compiler fuses adjacent stream operators into a single pass, eliminating intermediate allocations.

Streams + Agents

Agent output channels are streams — all stream operators work on agent outputs:

sensor.readings |> filter(fn(t) { t > 100.0 }) |> throttle(5000) |> map(fn(t) { "ALERT: ${t}" })