RosettaCodeData/Task/Run-length-encoding/Raku/run-length-encoding.raku

8 lines
241 B
Raku
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
sub encode($str) { $str.subst(/(.) $0*/, { $/.chars ~ $0 }, :g) }
sub decode($str) { $str.subst(/(\d+) (.)/, { $1 x $0 }, :g) }
my $e = encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW');
say $e;
say decode($e);