Streams
A stream produces a sequence of typed values asynchronously using yield.
Declaration
cleat
stream tokens(prompt: string) -> string needs { llm } buffer: 16 { yield "hello " yield "world" }
Clauses
| Clause | Required | Description |
|---|---|---|
needs { ... } | no | Effect requirements |
buffer: | yes | Go channel buffer size |
Producing values
Use yield inside a stream body to emit values:
cleat
stream countdown(n: int) -> int buffer: 8 { for i in range(n, 0) { yield i } }
Consuming streams
Use for...in to iterate over stream values:
cleat
for token in llm.tokens("Tell me about Cleat") { io.print(token) } io.println("") // final newline
The loop runs until the stream body completes, at which point the channel is closed and the loop exits.
How it works
- The stream body runs in a goroutine
yieldpushes values to a buffered Go channel- The consumer reads with
for...in(which translates tofor v := range ch) - When the body completes, the channel is closed automatically