P09 - Pack consecutive duplicate elements of a list into sublists.

Author: Ryan Connelly

Example

> 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

Source code: 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;