定義為
multi roundrobin(List:D: --> Seq)
用法
roundrobin LISTS
Round-Robin Merge Two Lists of Different Length
roundrobin很像 zip。不同之處是, roundrobin
不會在用光元素的列表上停止而是僅僅跳過任何未定義的值:
my @a = 1;
my @b = 1..2;
my @c = 1..3;
for flat roundrobin(@a, @b, @c) -> $x { $x.say } # 1,1,1,2,2,3
它只是跳過了未定的值, 直到最長的那個列表的元素用完。
my @list1 = 'a' .. 'h';
my @list2 = <x y>;
say flat roundrobin @list1, @list2; # a x b y c d e f g h
roundrobin 返回的是一列 Seq
, 所以使用 flat 進行展開。
my @list1 = 'a' .. 'h';
my @list2 = <x y>;
my $n = 3;
say flat roundrobin @list1.rotor($n - 1, :partial), @list2;
# >>>
# OUTPUT?a b x c d y e f g h?
.rotor
方法把一個列表分解為子列表。
交叉兩個字符串中的字符
#Given:
u = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
l = 'abcdefghijklmnopqrstuvwxyz'
#Wanted:
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
方法一:
say join '', (u.comb Z l.comb);
方法二:
say [~] (u.comb Z l.comb);
方法三
say [~] flat (u.comb Z l.comb);