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

60

Comments

  1. Each .emit delivers a value to the tap, which adds it to $total.

  2. After the three emissions the total is 10 + 20 + 30, that is 60.

Course navigation

Sum a live feed   |   react and whenever