Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproducing the elements of an array practically in Raku

Tags:

raku

How to reproduce each element of an array x times?

For instancefor my @a=<blu red>; and x=5, the result should look like

(blu blu blu blu blu red red red red red)

I've come up with this

say flat map { ($_, $_, $_, $_, $_) }, @a;

but of course for arbitrary values of x, it's not practical.

How to do it practically? Thank you.

like image 748
Lars Malmsteen Avatar asked Nov 16 '19 21:11

Lars Malmsteen


1 Answers

Try using the infix xx operator like this:

my @a=<blu red>;
my $x = 5;
my @b = @a.map({ $_ xx $x }).flat;
say @b;

Output:

[blu blu blu blu blu red red red red red]

Edit:

.. or simply use flatmap (though the documentation says the use of flatmap it is discouraged)

my @b = @a.flatmap({ $_ xx $x });
like image 56
Håkon Hægland Avatar answered Oct 23 '22 04:10

Håkon Hægland