Ordered partitions: Difference between revisions

m
→‎{{header|Perl}}: future-proof for 5.36
m (syntax highlighting fixup automation)
m (→‎{{header|Perl}}: future-proof for 5.36)
Line 1,882:
 
=={{header|Perl}}==
===Threaded Generator Method===
Code 1: threaded generator method. This code demonstrates how to make something like Python's
generators or Go's channels by using Thread::Queue. Granted, this is horribly inefficient, with constantly creating and killing threads and whatnot (every time a partition is created, a thread is made to produce the next partition, so thousands if not millions of threads live and die, depending on the problem size). But algorithms are often more naturally expressed in a coroutine manner -- for this example, "making a new partition" and "picking elements for a partition" can be done in separate recursions cleanly if so desired. It's about 20 times slower than the next code example, so there.
 
<syntaxhighlight lang="perl">use Thread 'async'strict;
use warnings;
use Thread 'async';
use Thread::Queue;
 
Line 1,911 ⟶ 1,914:
};
 
$q = new Thread::Queue->new;
(async{ &$gen; # start the main work load
$q->enqueue(undef) # signal that there's no more data
Line 1,927 ⟶ 1,930:
print "@$a | @$b | @$rb\n";
}
}</syntaxhighlight>
}
</syntaxhighlight>
 
Code 2: ===Recursive solution.===
{{trans|Raku}}
<syntaxhighlight lang="perl">use List::Util 1.33 qw(sum pairmap)strict;
use warnings;
use List::Util 1.33 qw(sum pairmap);
 
sub partition {
2,392

edits