Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Iterators / Exercises / Gather the multiples of three

Solution: Gather the multiples of three

Here is a possible solution to the task.

Code

my @threes = gather {
    for 1..15 {
        take $_ if $_ %% 3;
    }
}

say @threes;

🦋 You can find the source code in the file gather-evens.raku.

Output

[3 6 9 12 15]

Comments

  1. The loop visits every number, but take runs only when $_ %% 3 is true.

  2. So only the multiples of three are collected into the list.

Course navigation

Gather the multiples of three   |   Gather with a condition