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