RosettaCodeData/Task/Run-length-encoding/Perl-6/run-length-encoding.pl6

8 lines
241 B
Raku
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
sub encode($str) { $str.subst(/(.) $0*/, { $/.chars ~ $0 }, :g) }
2013-04-10 23:57:08 -07:00
2016-12-05 22:15:40 +01:00
sub decode($str) { $str.subst(/(\d+) (.)/, { $1 x $0 }, :g) }
2013-04-10 23:57:08 -07:00
my $e = encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW');
say $e;
say decode($e);