如何在 perl 6 中“展平"列表列表?
问题描述:
假设我想要 a、b 和 c 中 2 个字母的所有排列.
Let's say I want all permutations of 2 letters out of a, b and c.
我能做到:
my @perm = <a b c>.combinations(2)».permutations;
say @perm;
# [((a b) (b a)) ((a c) (c a)) ((b c) (c b))]
这很接近,但不完全是我需要的.我如何展平"它以便我得到:
which is close, but not exactly what I need. How do I "flatten" this so that I get:
# [(a b) (b a) (a c) (c a) (b c) (c b)]
?
答
另见 实现我(OP)想要的更好的方法".
另见一些可能的解决方案";回答我怎样才能完全展平 Raku 列表(列表(列表)......)"?问题.
my \perm = <a b c>.combinations(2)».permutations;
say perm; # (((a b) (b a)) ((a c) (c a)) ((b c) (c b)))
say perm[*]; # (((a b) (b a)) ((a c) (c a)) ((b c) (c b)))
say perm[*;*]; # ((a b) (b a) (a c) (c a) (b c) (c b))
say perm[*;*;*] # (a b b a a c c a b c c b)
注意事项
我使用了一个非印记变量,因为我认为对于那些不了解 Raku 的人来说,这更清楚一些.
Notes
I used a non-sigil'd variable because I think it's a bit clearer what's going on for those who don't know Raku.
我没有将下标附加到原始表达式中,但我可以:
I didn't append the subscript to the original expression but I could have:
my \perm = <a b c>.combinations(2)».permutations[*;*];
say perm; # ((a b) (b a) (a c) (c a) (b c) (c b))