P09 - 将列表中连续的重复元素打包成子列表。

作者:Ryan Connelly

示例

> say pack-consecutive-dups(<a a a a a b b c b d e e>.list).perl;
> (["a", "a", "a", "a", "a"], ["b", "b"], ["c"], ["b"], ["d"], ["e", "e"]).list

源代码: P09-topo.pl

use v6;

sub pack-consecutive-dups(@list)
{
    gather while @list.elems
    {
        my $last = @list[0];

        take [
            gather while @list.elems
                   and @list[0] ~~ $last
            {
                take ($last = @list.shift)
            }
        ]
    }.list
}

say pack-consecutive-dups([<a a a a a b b c b d e e>]).perl;