LZW compression: Difference between revisions

m
→‎{{header|Raku}}: updated raku programming solution ; add unicode support
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|Raku}}: updated raku programming solution ; add unicode support)
Line 4,270:
 
=={{header|Raku}}==
(formerly Perl 6) I just came across [https://stackoverflow.com/questions/30531078/ this SO question] by chance hence the update. Notably the ancestor Perl entry simply works without any further tweak.
(formerly Perl 6)
{{trans|Perl}}
<lang perl6>sub# compress(Str20200421 $uncompressedUpdated -->Raku Seq)programming solution {; add unicode support
 
sub compress(Str $uncompressed --> Seq) {
my $dict-size = 256;
my %dictionary = (.chr => .chr for ^$dict-size);
Line 4,278 ⟶ 4,280:
my $w = "";
gather {
for $uncompressed.encode('utf8').list.chrs.comb -> $c {
my $wc = $w ~ $c;
if %dictionary{$wc}:exists { $w = $wc }
Line 4,297 ⟶ 4,299:
my $w = shift @compressed;
join( '',Blob.new: flat ( gather {
take $w;
for @compressed -> $k {
Line 4,308 ⟶ 4,310:
$w = $entry;
}
} )».ords ).decode('utf-8')
}
}
say my @compressed = compress('TOBEORNOTTOBEORTOBEORNOT');
say decompress(@compressed);
 
my $decompressed = decompress(@compressed);
@compressed = compress('こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵');
say $decompressed;</lang>
my $decompressed =say decompress(@compressed);</lang>
{{out}}
<pre>
[T O B E O R N O T 256 258 260 265 259 261 263]
TOBEORNOTTOBEORTOBEORNOT
こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵
</pre>
 
354

edits