# "Channels stream values between tasks": a producer sends the squares of# 1 to 5, the consumer sums them until the channel is closed and drained.use io;
usetask;
usechannel;
def ch aschannelofintinitchannel.make(4);
def producer astaskofintinitspawn {
for (def n in1..6) { channel.send($ch, $n * $n); }
channel.close($ch);
return0;
};
def sum asintinit0;
while (true) {
# recv throws once the channel is closed and drainedtry { $sum = $sum + channel.recv($ch); } catch (e) { break; }
}
task.wait($producer);
io.printf("sum %d\n", $sum); # 55