This works:
bastille% perl6 -e 'my @squares = (1...*).map({ $_ ** 2 }); say @squares[0..^10].join: ", "'
1, 4, 9, 16, 25, 36, 49, 64, 81, 100
However this doesn't:
bastille% perl6 -e 'my @squares <== map { $_ ** 2 } <== 1...*; say @squares[0..^10].join: ", "'
Cannot push a lazy list onto a Array
in block <unit> at -e line 1
Why does this throw? Is there a way that lazy lists can be used with feeds?
The feed operator <==
appends elements to the array.
my @s <== ^3;
@s <== <a>..<c>;
say @s
[0 1 2 a b c]
So
my @squares <== map { $_ ** 2 } <== 1...*;
works similarly as
my @squares.append: map * ** 2, 1..*;
You can use the item assignment operator =
with brackets
my @squares = ( map { $_ ** 2 } <== 1..* );
or the next little hack
[=] my @squares <== map { $_ ** 2 } <== 1..*;
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