P09 - 将列表中连续重复的元素打包成子列表。
作者:Scott Penrose
规范
P09 (**) Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists.
示例
> pack_dup(<a a a a b c c a a d e e e e>).perl.say [["a","a","a","a"],["b"],["c","c"],["a","a"],["d"],["e","e","e","e"]]
源代码: P09-scottp.pl
use v6; my @l = <a a a a b c c a a d e e e e>; sub packit (@in) { my @out; my @last = [shift @in,]; for @in -> $t { if (@last[0] ne $t) { push @out, @last; @last := [$t,]; } else { push @last, $t; } } if (@last.elems) { push @out, @last; } return @out; } say ~@l; say packit(@l).perl;