Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / react and whenever / Exercises / Upper-case with react
Solution: Upper-case 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-upcase.raku.
Output
[A B C]Comments
The
wheneverbody runs once per value, upper-casing it and pushing it onto@collected.reactwaits for the single supply to finish, so by the timesayruns the array holds all three values in order:[A B C].A supply has list-like methods of its own, so you could upper-case in the stream instead of in the body —
whenever Supply.from-list('a', 'b', 'c').map(*.uc) { @collected.push($_) }— and the effect is the same..mapon a supply transforms each value as it flows past; the choice is simply whether the transformation belongs to the stream or to the reaction.