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

Cheers - L~R

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.

Replies are listed 'Best First'.
Re: Perl6 Infinite Lazy Lists
by stvn (Monsignor) on May 02, 2005 at 20:30 UTC

    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
      I Believe that P4 has local (my) subroutines now. Also, I always find it ugly to pass $_ explicitly. So that $by_n sub and the subsequent map could be written:
      my sub by_n ($n := $_) { my $k = 0; return -> $x { $x ?? $k += $n +:: $k } }; @list = @list.map( &by_n );
      Come to think of it, I think there's an in-place map operator, too:
      @list.=map( &by_n );
      --Dave
      Opinions my own; statements of fact may be in error.
        dpuu

        Very nice, I forgot about the in-place operations (which unfortunatly don't yet work in Pugs through).

        -stvn
      This may just be a syntax misunderstanding on my part, but why do you need the colon in the map call?
        Errto

        The colon is a by-product of the invocant syntax. To be honest, I am not sure why it is like that, that is a question best left the perl6 language gurus :)

        -stvn
      A little further cleanup, taking advantage of things that now work in Pugs, and golfing in a number of places (added to pugs examples file):
      sub lazy_merge3 (@list is copy) returns Sub { my $last = 0; my &by_n = -> $n { my $k = 0; -> $x { $x ?? $k += $n :: $k } }; @list .= map:{ by_n $_ }; sub { my $low; for @list -> $sub { my $val = $sub(); $val = $sub(<next>) if $val <= $last; $low = $val unless $low && $low < $val; } $last = $low; } }