How can I use “map” inside a “for” loop in Perl 6?

冷暖自知 提交于 2019-12-05 03:57:41

Let's take a look at

say (map { say $_ }, 1..2).WHAT;

This tells us &map returns a Seq, which is a lazy construct.

Additionally, the last statement in the body of a for loop is used to aggregate its return value, cf

my @list = do for 1..3 {
    map { say $_ }, 1..2;
}

say .WHAT for @list;

If 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:

for 1..3 {
    sink map { say $_ }, 1..2;
}

Alternatively, just use another for loop:

for 1..3 {
    .say for 1..2;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!