LZW compression: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (→{{header|Raku}}: updated raku programming solution ; add unicode support) |
||
Line 4,270: | Line 4,270: | ||
=={{header|Raku}}== |
=={{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}} |
{{trans|Perl}} |
||
<lang perl6> |
<lang perl6># 20200421 Updated Raku programming solution ; add unicode support |
||
sub compress(Str $uncompressed --> Seq) { |
|||
my $dict-size = 256; |
my $dict-size = 256; |
||
my %dictionary = (.chr => .chr for ^$dict-size); |
my %dictionary = (.chr => .chr for ^$dict-size); |
||
Line 4,278: | Line 4,280: | ||
my $w = ""; |
my $w = ""; |
||
gather { |
gather { |
||
for $uncompressed.comb -> $c { |
for $uncompressed.encode('utf8').list.chrs.comb -> $c { |
||
my $wc = $w ~ $c; |
my $wc = $w ~ $c; |
||
if %dictionary{$wc}:exists { $w = $wc } |
if %dictionary{$wc}:exists { $w = $wc } |
||
Line 4,297: | Line 4,299: | ||
my $w = shift @compressed; |
my $w = shift @compressed; |
||
( Blob.new: flat ( gather { |
|||
take $w; |
take $w; |
||
for @compressed -> $k { |
for @compressed -> $k { |
||
Line 4,308: | Line 4,310: | ||
$w = $entry; |
$w = $entry; |
||
} |
} |
||
} )».ords ).decode('utf-8') |
|||
} |
|||
} |
} |
||
my @compressed = compress('TOBEORNOTTOBEORTOBEORNOT'); |
say my @compressed = compress('TOBEORNOTTOBEORTOBEORNOT'); |
||
say @compressed; |
say decompress(@compressed); |
||
⚫ | |||
@compressed = compress('こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵'); |
|||
say $decompressed;</lang> |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
T O B E O R N O T 256 258 260 265 259 261 263 |
[T O B E O R N O T 256 258 260 265 259 261 263] |
||
TOBEORNOTTOBEORTOBEORNOT |
TOBEORNOTTOBEORTOBEORNOT |
||
こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵 |
|||
</pre> |
</pre> |
||