Channels and Pipelines
Channel[T] connects concurrent producers and consumers with explicit close behavior.
Channel[t]
channel(capacity) creates a bounded channel for values of type T.
channel.brp
ch: Channel[Int] = channel(10)Send/recv
send blocks when the channel is full; recv returns Option[T] and returns None when the channel is closed.
send-recv.brp
_ = send(ch, 42)
value: Option[Int] = recv(ch)Close
close tells receivers that no more values will arrive.
close.brp
close(ch)
match recv(ch):
None: print("closed")
Some(v): print(v.to_string())For-in Over Channels
A for loop over a channel receives values until close.
channel-loop.brp
for value in ch:
total += valueExample
pipeline.brp
func produce(ch: Channel[Int], start: Int, end: Int) -> Void:
var i: Int = start
while i < end:
_ = send(ch, i)
i += 1
func consume(ch: Channel[Int]) -> Int:
var total: Int = 0
for n in ch:
total += n
total
func main(args: List[String]) -> Int:
ch: Channel[Int] = channel(10)
concurrent:
first = produce(ch, 0, 3)
second = produce(ch, 3, 5)
close(ch)
print(consume(ch).to_string())
0
Try It
terminal
blorp run pipeline.brp