Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Iterators / gather and take
Quiz — gather and take
What does the following program print?
my @a = gather {
for 1..5 {
take $_ if $_ > 3;
}
}
say @a;| 0 | [1 2 3 4 5] |
| 1 | [4 5] |
| 0 | [1 2 3] |
| 0 | [3 4 5] |
The take runs only when $_ > 3, so only
4 and 5 are added to the list. Everything else
is skipped, giving [4 5].
Course navigation
← gather and take | Lazy gather →