All,
For any of you that missed it,
Pugs version 6.2.2 was
released yesterday. To help spread advocacy, I want to start answering questions here at the Monastery with both p5 and p6 answers much like
merlyn did way back in the day. The trouble is that my p6 fu isn't very strong yet, so I have been
practicing. This is my latest attempt which is a port of
this.
Stop doubting and start believing.
use v6;
sub lazy_merge (@list is rw) returns Ref {
my $last = 0;
my $by_n = sub { my ($n, $k) = (shift(@_), 0); return { @_[0] ?? $
+k += $n :: $k } };
for @list { $_ = $by_n( $_ ) }
return sub {
my $low;
for ( @list ) {
my $val = $_();
$val = $_( 'next' ) if $val <= $last;
$low = $val if (! defined $low) || ($val < $low);
}
return $last = $low;
};
}
my $end = @*ARGS[0] // 22;
my @prime = (2, 3, 5);
my $next = lazy_merge( @prime );
for 1..$end { say $next() };
As it has been added as an example in the
Pugs distribution, you can always see the latest version
here
It is worth mentioning that there very well may be a more p6ish way to write this. It is usually necessary to crawl before running.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.