我如何解决"The iterator of this Seq is already in use"?

How do I solve "The iterator of this Seq is already in use"?

此代码:

sub MAIN(Int $N = 128)
{
    my @pascal = ((1,), { (0, |@^a) Z+ (|@^a, 0) } ... *);

    my @top = @pascal[^$N];
    say "Percentage of odd numbers in the first $N rows: ",
        (100 × @top».grep(* % 2).sum / @top.sum).fmt('%.12f%%');
}

给我一个错误:

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 sub MAIN at ./pascal1 line 8
  in block <unit> at ./pascal1 line 1

知道如何解决吗?我试过在几个地方添加 .cache,但没有成功。

作为序列一部分的块正在创建序列。您应该能够像这样缓存它:

{ ( (0, |@^a) Z+ (|@^a, 0) ).cache }

Zreturns一个Seq

Seq 产生它的下一个值时,它会丢弃前一个值。

因此您通常只能从 Seq 中获取值一次。

您拥有的区块 {…} 通过查看它生成的前一个 Seq 来工作。所以有一个问题。您要么看到 Seq 中的内容,要么 ... 运算符看到 Seq

中的内容

问题是,您可能不希望 Z 的结果是 Seq,您希望它是 List.

毕竟你用 List (1,).

启动了 ... 序列生成器
((1,), { ((0, |@^a) Z+ (|@^a, 0)).List } ... *)