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

Solution: Gather the cubes

Here is a possible solution to the task.

Code

my @cubes = gather {
    take $_ ** 3 for 1..4;
}

say @cubes;

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

Output

[1 8 27 64]

Comments

  1. The take runs once for each number, contributing its cube ($_ ** 3).

  2. The gather block evaluates to the list of all taken values.

Course navigation

Gather the cubes   |   Gather the multiples of three