Suppose I define a lazy, infinite array using a triangular reduction at the REPL, with a single element pasted onto the front:
> my @s = 0, |[\+] (1, 2 ... *)
[...]
I can print out the first few elements:
> @s[^10]
(0 1 3 6 10 15 21 28 36 45)
I'd like to move the zero element inside the reduction like so:
> my @s = [\+] (0, |(1, 2 ... *))
However, in response to this, the REPL hangs, presumably by trying to evaluate the infinite list.
If I do it in separate steps, it works:
> my @s = 0, |(1, 2 ... *)
[...]
> ([\+] @s)[^10]
(0 1 3 6 10 15 21 28 36 45)
Why doesn't the way that doesn't work...work?
Short answer:
It is probably a bug.
Long answer:
(1, 2 ... *)
produces a lazy sequence because it is obviously infinite, but somehow that is not making the resulting sequence from being marked as lazy.
Putting a sequence into an array @s
causes it to be eagerly evaluated unless it is marked as being lazy.
Quick fix:
Append lazy
to the front.
> my @s = [\+] lazy 0, |(1, 2 ... *)
[...]
> @s[^10]
(0 1 3 6 10 15 21 28 36 45)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With