Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use feeds with lazy lists?

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?

like image 824
Kaiepi Avatar asked May 13 '19 22:05

Kaiepi


1 Answers

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..*;
like image 155
wamba Avatar answered Sep 16 '22 17:09

wamba