Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / Live and on-demand supplies / Exercises / Sum a live feed
Solution: Sum a live feed
Here is a possible solution to the task.
Code
my $supplier = Supplier.new;
my $total = 0;
$supplier.Supply.tap(-> $v { $total += $v });
$supplier.emit(10);
$supplier.emit(20);
$supplier.emit(30);
say $total;🦋 You can find the source code in the file live-sum.raku.
Output
60Comments
Each
.emitdelivers a value to the tap, which adds it to$total.After the three emissions the total is
10 + 20 + 30, that is60.