in reply to Perl6 Infinite Lazy Lists

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

Replies are listed 'Best First'.
Re^2: Perl6 Infinite Lazy Lists
by dpuu (Chaplain) on May 03, 2005 at 01:14 UTC
    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
Re^2: Perl6 Infinite Lazy Lists
by Errto (Vicar) on May 03, 2005 at 00:34 UTC
    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
Re^2: Perl6 Infinite Lazy Lists
by geoffb (Novice) on Jun 04, 2005 at 23:15 UTC
    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; } }