Skip to content

merge

merge : [ST] ST

merge streams concatenates a list of ST streams sequentially. All elements from the first stream appear before any from the second, and so on.

Implemented as:

merge = streams: st.flatten (st.fromList streams);

st.fromList turns the list into a stream-of-streams; st.flatten flatMaps with identity, concatenating them.

merge [ (st 1 2 3) (st 4 5) (st 6) ]
# → ST [1 2 3 4 5 6]

Combine multiple actor outboxes into one downstream inbox:

sender-a = actor (n: reply (n + 1)) { inbox = st 0 1 2; };
sender-b = actor (n: reply (n * 10)) { inbox = st 1 2; };
downstream = actor process {
inbox = merge [ sender-a.outbox sender-b.outbox ];
};

sender-a emits 1 2 3; sender-b emits 10 20. Downstream sees 1 2 3 10 20 — all of sender-a first, then all of sender-b.

Use merge to extend an inbox with static messages:

bootstrapped-c =
{ inbox }:
counter-c { inbox = merge [ inbox (st "inc" "inc") ]; };

External messages first, then the two bootstrap "inc" commands appended after.

  • Order is deterministic and matches list order.
  • merge does not interleave streams — it concatenates. For true interleaving, you would need a different combinator.
  • An empty list merge [] produces an empty stream.
Contribute Community Sponsor