Raku-Kurs / Funktionale, nebenläufige, reaktive und Web-Programmierung / Funktionale Programmierung / Generatoren / gather und take

Quiz — gather und take

Was gibt das folgende Programm aus?

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]

Das take läuft nur, wenn $_ > 3, der Liste werden also nur 4 und 5 hinzugefügt. Alles andere wird übersprungen, was [4 5] ergibt.

Kursnavigation

gather und take   |   Lazy gather