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

Z returns a Seq

As a Seq produces it's next value, it throws the previous one away.

So you can generally only get the values from a Seq once.

The block {…} you have works by looking at the previous Seq it generated. So there is an issue. Either you get to see what is in those Seq, or the ... operator gets to see what is in the Seq


The thing is, you probably didn't want the result of Z to be a Seq, you wanted it to be a List.

After all you start off the ... sequence generator with a List (1,).

((1,), { ((0, |@^a) Z+ (|@^a, 0)).List } ... *)

The block you have as part of your sequence is creating a Seq. You should be able to cache it like this:

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

Tags:

Raku