我正在使用以下代码尝试供稿运算符:
my $limit=10000000;
my $list=(0,1...4);
sub task($_,$name) {
say "Start $name on $*THREAD";
loop ( my $i=0; $i < $limit; $i++ ){};
say "End $name";
Nil;
}
sub stage( &code, *@elements --> Seq) {
map(&code, @elements);
}
sub feeds() {
my @results;
$list
==> map ({ task($_, "stage 1"); say( now - ENTER now );$_+1})
==> map ({ task($_, "stage 2"); say( now - ENTER now );$_+1})
==> stage ({ task($_, "stage 3"); say( now - ENTER now );$_+1})
==> @results;
say @results;
}
feeds();
运行时的输出为:
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.7286811
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.59053989
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.5955893
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.59050998
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.59472201
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.5968531
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.5917188
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.587358
Start stage 1 on Thread<1>(Initial thread)
End stage 1
0.58689858
Start stage 2 on Thread<1>(Initial thread)
End stage 2
0.59177099
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.8549498
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.8560015
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.77634317
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.6754558
Start stage 3 on Thread<1>(Initial thread)
End stage 3
3.672909
[3 4 5 6 7]
@result
中的结果正确(来自$list
的输入以3递增3)
前两个阶段的输出拉/交替,但是直到完成第2阶段的所有输入后,第三阶段才会执行
我的map子包装器出现问题了吗?
与直接调用map相比,在包装器中评估所需的时间也明显更长。
感谢您的帮助。
答案 0 :(得分:6)
slurpy参数正在等待消耗传递给它的整个序列。考虑以下两者之间的区别:
perl6 -e 'sub foo($) { }; my $items := gather for 1..Inf { say $_ }; foo($items);'
perl6 -e 'sub foo(*@) { }; my $items := gather for 1..Inf { say $_ }; foo($items);'
第一个示例将结束,第二个示例将永远结束。因此,您需要将舞台功能更改为:
sub stage( &code, $elements --> Seq) {
map(&code, $elements);
}