Ordered partitions: Difference between revisions
m
→{{header|Perl}}: future-proof for 5.36
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36) |
||
Line 1,882:
=={{header|Perl}}==
===Threaded Generator Method===
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
use warnings;
use Thread 'async';
use Thread::Queue;
Line 1,911 ⟶ 1,914:
};
$q =
(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>
{{trans|Raku}}
<syntaxhighlight lang="perl">use
use warnings;
use List::Util 1.33 qw(sum pairmap);
sub partition {
|