Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / react and whenever / Exercises / Collect with react

Solution: Collect with react

Here is a possible solution to the task.

Code

my @collected;

react {
    whenever Supply.from-list('a', 'b', 'c') {
        @collected.push($_.uc);
    }
}

say @collected;

🦋 You can find the source code in the file react-sum.raku.

Output

[A B C]

Comments

  1. The whenever body runs once per value, upper-casing it and pushing it onto @collected.

  2. react waits for the single supply to finish, so by the time say runs the array holds all three values in order: [A B C].

Course navigation

Collect with react   |   Collect with react