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(1, 2, 3) {
        @collected.push($_);
    }
}

say @collected;

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

Output

[1 2 3]

Comments

  1. Each value the supply emits is pushed onto @collected.

  2. The react block waits until the stream is done, so the array is complete before it is printed.

Course navigation

Collect with react   |   Two whenevers