P09 - 将列表中连续重复的元素打包成子列表。
作者:罗布·伊格尔斯通
规范
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-rje.pl
use v6;
# Robert Eaglestone 22 sept 09
#
# My first Perl6 script - I'm sure this can be done better
#
my @in = <a a a a b c c a a d e e e e>;
my @out = pack_dup( @in );
@out.perl.say;
sub pack_dup(@in) {
my @out = [ [@in.shift], ];
for @in -> $elem {
push @out, [] if $elem ne @out[*-1][0];
push @out[*-1], $elem;
}
return @out;
}
Perl 6 示例