P11 - 改进的游程编码。

作者: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"])

源代码: P11-topo.pl

use v6;

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>]).list.perl;