Corso di Raku / Programmazione funzionale, concorrente, reattiva e web / Programmazione funzionale / Generatori / gather e take

Quiz — gather e take

Cosa stampa il programma seguente?

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]

Il take gira solo quando $_ > 3, quindi all’elenco vengono aggiunti solo 4 e 5. Tutto il resto viene saltato, dando [4 5].

gather e take   |   gather pigro