【问题标题】:Cumulative Z op throws a "The iterator of this Seq is already in use/consumed by another Seq"累积 Z op 抛出“此 Seq 的迭代器已被另一个 Seq 使用/消耗”
【发布时间】:2019-01-31 18:05:18
【问题描述】:

这是解决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

知道如何解决这个问题吗?

【问题讨论】:

  • 这对我来说似乎是一个错误:因为您没有在代码中明确使用任何Seq,所以您应该没有办法让这个错误发生。请创建一个问题。
  • 解决方法:([\Z+] @bitfields).map(*.list);不确定这是否真的是一个错误 - 序列是由 Z 创建的

标签: raku


【解决方案1】:

首先xx 创建一个序列

say (Bool.pick xx 3).^name; # Seq

所以你可能想把它变成一个数组(或列表)。

for ^3 -> $i {
    @bitfields[$i] = [Bool.pick xx 3];
}

我会使用.roll(3),而不是.pick xx 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多