P13 - 直接游程编码。

作者:Ryan Connelly

示例

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

源代码: 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(<a a a a b c c a a d e e e e>.Array).list.perl;