Course of Raku / Advanced /
Control flow / gather and
take / Exercises / Squares
Solution: Squares
Here is a possible solution to the task.
Code
my @squares = gather for 1..6 {
take $_ ** 2 if $_ %% 2;
};
say @squares;🦋 You can find the source code in the file squares.raku.
Output
[4 16 36]Comments
The
if $_ %% 2guard decides whether to take, and$_ ** 2decides what to take. Only the even numbers2,4,6pass the guard.Their squares
4,16,36are the only values handed totake, sogathercollects exactly those. Doing the filtering and the transforming together in onetakeis something a plainmapcannot express as cleanly.
Course navigation
← Squares | Gather until full →