这是另一种解决方法previous question
my @bitfields;
for ^3 -> $i {
@bitfields[$i] = Bool.pick xx 3;
}
my @total = [\Z+] @bitfields;
say @total;
它应该将每一行压缩添加到下一行,并累积该值。但是,这会产生错误
The iterator of this Seq is already in use/consumed by another Seq
(you might solve this by adding .cache on usages of the Seq, or
by assigning the Seq into an array)
in block <unit> at vanishing-total.p6 line 8
知道如何解决这个问题吗?
最佳答案
第一 xx
创建一个序列
say (Bool.pick xx 3).^name; # Seq
所以你可能想把它变成一个数组(或列表)。
for ^3 -> $i {
@bitfields[$i] = [Bool.pick xx 3];
}
也而不是
.pick xx 3
, 我会用 .roll(3)
.for ^3 -> $i {
@bitfields[$i] = [Bool.roll(3)];
}
zip (
Z
) 元运算符也创建序列。say ( [1,2] Z [3,4] ).perl;
# ((1, 3), (2, 4)).Seq
say ( [1,2] Z+ [3,4] ).perl
# (4, 6).Seq
所以
[\Z+]
对于两个输入,甚至无法按照您想要的方式工作。say [\Z+]( [1,2], [3,4] ).perl;
# (Seq.new-consumed(), Seq.new-consumed()).Seq
say [\Z+]( 1, 2 ).perl;
# (Seq.new-consumed(), Seq.new-consumed()).Seq
如果你做一些事情来缓存中间值,它确实有效。
say [\Z+]( [1,2], [3,4] ).map(*.cache).perl
# ((3,), (4, 6)).Seq
say [\Z+]( [1,2], [3,4] ).map(*.list).perl
# ((3,), (4, 6)).Seq
say [\Z+]( [1,2], [3,4] ).map(*.Array).perl
# ([3], [4, 6]).Seq
您可能还想在前面添加一个列表和一个
.skip
.my @bitfields = [
[Bool::True, Bool::True, Bool::False],
[Bool::False, Bool::False, Bool::True ],
[Bool::False, Bool::True, Bool::True ]
];
say [\Z+]( @bitfields ).map(*.List)
# ((2) (1 1 1) (1 2 2))
say [\Z+]( (0,0,0), |@bitfields ).map(*.List).skip
# ((1 1 0) (1 1 1) (1 2 2))
如果不需要中间结果
[Z+]
会工作得很好。say [Z+]( Bool.roll(3) xx 3 ) for ^5;
# (0 1 3)
# (1 2 1)
# (1 0 3)
# (0 1 2)
# (1 2 2)
关于raku - 累积 Z op 抛出 "The iterator of this Seq is already in use/consumed by another Seq",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54466736/