raku - 累积 Z op 抛出 "The iterator of this Seq is already in use/consumed by another Seq"

标签 raku

这是另一种解决方法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/

相关文章:

equation - 在 Raku 中求解指数方程

object - 有谁知道为什么 TWEAK 例程在 BUILD 例程之前被命中?

grammar - 使用语法解析可能嵌套的支撑项

xml - raku 语法的标记不会命中文档的第一次出现,但会命中类似的后续出现

MacOS 上的 Raku Comma IDE 无法启动

mysql - 在 rakudo-star docker 镜像上安装带有 DBIish 的 mysql 的安装要求

traits - 移相器特性何时运行?

loops - 在 Raku 的内部循环中使用循环的位置参数

raku - Perl 6 中的 "await do"是什么?

raku - 如何设置类的默认参数化?