First, let me say I applaud your advocacy efforts. ++ to that :)

Second,.. I will take a stab it further perl6-ification.

use v6; sub lazy_merge (@list is rw) returns Sub { my $last = 0; my $by_n = sub ($n) { my $k = 0; return -> $x { $x ?? $k += $n :: +$k } }; @list = @list.map:{ $by_n( $_ ) }; return sub { my $low; for ( @list ) -> $sub { my $val = $sub(); $val = $sub(<next>) if $val <= $last; $low = $val if !$low.defined || $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() };
Most of the improvement I think is in the $by_n closure. IMHO there is really no need to use @_ anymore (unless of course you really want to). Having subroutine parameters I think really helps to clean up the body of a sub. (BTW - for those who are not familiar the  -> $x {} is called a "pointy" sub, and basically is a bare block with parameters (think Ruby or even better Smalltalk)).

I also changed your return type to 'Sub' as that should be the proper type IIRC. (Also note that return types are not fully implemented in Pugs, so you could actually put 'Array', and it will still DWIM).

The other changes are somewhat more stylistic and therefore are just my personal preference. Changing the for (@list) loop into a map is just my bais towards a functional style of programming. I added the $sub topic to for ( @list ) -> $sub as well (again personal preference). And the $low.defined is just my bias towards OO style programming (even though it is not really OO, just invocant style syntax).

UPDATE
Changed $sub('next') to use the new qw operator $sub(<next>), just cause it's cooler :P

-stvn

In reply to Re: Perl6 Infinite Lazy Lists by stvn
in thread Perl6 Infinite Lazy Lists by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.