P13 - Direct run-length encoding.

Author: Ryan Connelly

Example

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

Source code: P13-topo.pl

use v6;

# I can't see the difference between this and P11.
# Perhaps I'm being stupid.

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

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

        take $count == 1 ?? $value !! [$count, $value];
    }
}

say encode(.Array).list.perl;