Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / Live and on-demand supplies / Exercises / Tuning in late
Solution: Tuning in late
Here is a possible solution to the task.
Code
my $supplier = Supplier.new;
my @got;
$supplier.emit(1);
$supplier.Supply.tap(-> $v { @got.push($v) });
$supplier.emit(2);
$supplier.emit(3);
say @got;🦋 You can find the source code in the file a-live-feed.raku.
Output
[2 3]Comments
The
Supplieris the sending end; its.Supplyis what you tap. A live supply broadcasts only to the taps listening at the moment of each emission.The
emit(1)happens before any tap exists, so it is lost — like tuning into a radio station after a song has played. Only2and3, emitted after the tap, are captured, giving[2 3].