I'm looking for a simpler solution.
I have a list of prefixes with corresponding suffixes and a list of roots.
my @prefixes = 'A'..'E';
my @suffixes = 'a'..'e';
my @roots = 1, 2;
I would like to make all the possible 'words': A1a
, B1b
...A2a
...E2e
.
my @words;
for @roots -> $r {
for @prefixes.kv -> $i, $p {
my $s = @suffixes[$i];
my $word = [~] $p, $r, $s;
@words.push: $word;
}
}
say @words; # [A1a B1b C1c D1d E1e A2a B2b C2c D2d E2e]
I suppose that it is possible to do it much easier using something like zip
or cross
, but can't figure out how...
My solution would be:
say @roots.map: |(@prefixes >>~>> * <<~<< @postfixes);
Create a WhateverCode for metaopping concatenation, slipping the result to get a Seq
with only scalar values at the end.
A few more ways to write it:
say @roots X[&join] (@prefixes Z @suffixes);
say @roots.map({ |(@prefixes Z @suffixes)».join($_) });
say @roots.map({ (@prefixes X~ $_) Z~ @suffixes }).flat;
say (|@prefixes xx *) Z~ (@roots X~ @suffixes);
my @formats = (@prefixes Z @suffixes).flat.map(* ~ '%s' ~ *);
say @formats X[&sprintf] @roots;
(Note: This one prints them in a different order.)
say do for @roots -> $root {
|do for (@prefixes Z @suffixes) -> [$prefix, $suffix] {
$prefix ~ $root ~ $suffix
}
}
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