Overview
Loops over values that suspending work produces: streams, and channels read until an end.
(import (std stream))
;; Pages, fetched one at a time as the loop wants them.
(def pages {streaming (fetch url) (:for url urls)})
{taking 10 page (:for page pages)} ; fetches ten pages, then stops
;; A producer written by hand.
(def ticks
(make-stream (bjoroutine (out)
(loop (:for i (range 0 3))
(:do (sync (timeout (seconds 1))) (emit! out i)))))))
(loop (:for t ticks) (:do (println (int->string t))))
A Seq cannot suspend: it is a .NET iterator, and an iterator
cannot also await. A Stream is the sequence for that case. Its
producer runs in a fiber of its own and hands each value over a channel,
so the producer can wait for anything, and the loop over it waits for the
producer.
The loop owns the producer. A stream is a recipe. Holding one
runs nothing. Each loop over it starts the producer, and stops it when the
loop ends: when the producer is done, at a :finish or an
:abandon, when a driver beside it runs out, at finding or
taking, at an exception or a ret. A producer stopped so raises
Cancelled where it waits, and its own releases run. The loop
returns only after the producer has unwound, so its releases have run
when the loop returns. This is also true when the loop is left because
its scope was cancelled: the wait runs in a with-shield.
The loop waits at most five seconds. A producer that computes without a
sync does not see the stop until it next waits. If the producer
has not unwound after five seconds, the loop returns, and the enclosing
scope waits for the producer. What the producer raises for its own reasons is raised
in the loop. A loop at an inner level gets a producer each time the
level starts.
Nothing is computed ahead. The producer computes a value only
when the loop asks for it: after each emit! it waits for the next
request. So a loop left early has cost exactly the values it took, and
port->stream in (std ports) leaves the port just after the
last item taken. One exception: a driver beside the stream that runs out
is seen after the stream has stepped, so that costs one value more.
A channel somebody else fills is read with in-chan, which
ends when an event commits, such as the producer’s
promise-join. That loop does not own the producer: leaving it
early leaves the producer and the channel as they are, for another
reader.
Where the loop runs. In a bjoroutine, each receive suspends. In
ordinary code it parks the thread, as sync/blocking does.
See also: Stream, make-stream, emit!, streaming, in-chan
Reference
Types
Stream
record ↑ Types
A sequence whose values a producer that may suspend emits. Each loop over it runs the producer afresh.
%a- The type of the values.
An Iterable: (:for x s) in a loop or a comprehension. Its
cursor is a StreamRun, made when the loop starts.
See also: make-stream, streaming, emit!, Iterable
Emitter
record ↑ Types
Where a stream’s producer sends its values, with emit!.
%a- The type of the values.
See also: emit!, make-stream
StreamRun
record ↑ Types
A loop’s cursor over a stream: the running producer, and the value the loop is at.
%a- The type of the values.
Made by iterable-start and ended by iterable-end or
iterable-cleanup. These stop the producer and wait until it has
unwound.
See also: Stream
Item
union ↑ Types
What a stream’s producer hands the loop: a value, the end, or what the producer raised.
%a- The type of the values.
Next- A value.
Done- The producer returned.
Failed- The producer raised this. The loop raises it again.
See also: Stream
ChanReads
record ↑ Types
The values received on a channel until an event commits: what in-chan makes.
%a- The type of the values.
%e- The type of the end event's value, which is dropped.
See also: in-chan
Functions
make-stream
function ↑ Functions
(: make-stream (-> (-bjo-> (Emitter %a) void) (Stream %a)))
(make-stream body)
A stream whose producer is body.
body- A bjoroutine given an Emitter. It emits each value with emit!, and the stream ends when it returns.
- returns
- The stream. Nothing runs until a loop over it starts.
(make-stream (bjoroutine (out)
(emit! out 1)
(emit! out 2)))
emit!
function ↑ Functions
(: emit! (-> (Emitter %a) %a void))
(emit! out v)
Hands a value to the loop over the stream, and waits until the loop asks for the next one.
out- The producer's Emitter.
v- The value.
- raises
Bjolang.Runtime.Cancelled - When the loop has ended. The producer then unwinds.
The producer goes on only when the loop asks for another value, so it computes nothing the loop does not take.
See also: make-stream
in-chan
function ↑ Functions
(: in-chan (-> (Chan %a) (Event %e) (ChanReads %a %e)))
(in-chan c end)
The values received on a channel, until an event commits.
c- The channel.
end- The event that ends the reading, such as (promise-join producer) or (timeout d).
- returns
- A ChanReads, for (:for x (in-chan c end)).
(def feeder (bjo (feed jobs urls)))
(loop (:for url (in-chan jobs (promise-join feeder)))
(:do (fetch url)))
Each step receives from c or waits for end, whichever
commits first. A channel is a rendezvous, so a producer that has
returned has handed over every value it sent: its promise is a safe end.
The loop does not own the producer. Leaving it early stops the reading only; what is not read is still there for another reader.
See also: Stream, promise-join, chan-recv
Macros
streaming
macro ↑ Macros
(streaming expr clause ...)
A stream of an expression for each round of a loop, which runs in the producer and may suspend.
expr- The value emitted each round.
clause ...- Loop clauses, as in a comprehension.
{streaming (fetch url) (:for url urls)}
{streaming i :when (odd? i) (:for i (range 0 10))}
Written as a comprehension, {streaming expr clause ...}. It is
seqing for work that suspends: the loop of the clauses runs in the
stream’s producer and emits expr each round, and a loose
:when gates it.
See also: Stream, make-stream