Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / Supplies / Exercises / Filter a supply

Solution: Filter a supply

Here is a possible solution to the task.

Code

my @out;
Supply.from-list(1..6).grep(* %% 2).tap(-> $v { @out.push($v) });
say @out;

🦋 You can find the source code in the file map-a-supply.raku.

Output

[2 4 6]

Comments

  1. grep(* %% 2) produces a new supply that passes on only the even values, just as grep filters a list.

  2. The tap collects those values into @out, giving [2 4 6].

Course navigation

Filter a supply   |   Count the emissions