I'm trying to use map
inside a for
loop, but this code is not working. This code should print 1
and 2
three times. But the code below produces no output. It will only partially work if you uncomment the exit
line. Any idea why? I tested it using the current Perl 6 version (Rakudo version 2015.12-79-g4bb47d5 built on MoarVM version 2015.12 and also the Windows version).
for (1..3) {
map { say $_ }, (1..2);
# exit;
}
Let's take a look at
This tells us
&map
returns aSeq
, which is a lazy construct.Additionally, the last statement in the body of a
for
loop is used to aggregate its return value, cfIf you add another statement after the call to
&map
, the call will be in 'sink context' and gets evaluated eagerly.This can also be forced explicitly:
Alternatively, just use another
for
loop: