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.
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 });
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