P10 - Run-length encoding of a list.

Author: Ryan Connelly

Example

> say encode(<a a a a a b b c b d e e>.list).perl;
([5, "a"], [2, "b"], [1, "c"], [1, "b"], [1, "d"], [2, "e"]).list

Source code: P10-topo.pl

use v6;

sub encode(@ls)
{
    my @list = @ls[*];
    gather while @list.elems {
        my $value = @list[0];
        my $count = 0;

        while @list.elems and @list[0] ~~ $value {
            $count++;
            shift @list
        }

        take [$count, $value];
    }
}

say encode(<a a a a a b b c b d e e>).list.perl;