Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Challenge: Another Infinite Lazy List

by Limbic~Region (Chancellor)
on Mar 22, 2005 at 00:19 UTC ( [id://441347]=note: print w/replies, xml ) Need Help??


in reply to Challenge: Another Infinite Lazy List


My solution in the root thread required was only minimally better than my original solution (to the wrong problem). This minimally tested version is vast improvement over both previous solutions.
#!/usr/bin/perl use strict; use warnings; my $end = shift || 22; my $next = lazy_merge( [ @ARGV ? @ARGV : (2, 3, 5) ] ); print $next->(), "\n" for 1 .. $end; sub lazy_merge { my ($list, $last) = (shift(), 0); my $by_n = sub { my ($n, $k) = (shift(), 0); return sub { $_[0] ? +$k += $n : $k } }; $_ = $by_n->( $_ ) for @$list; return sub { my $low; for ( @$list ) { my $val = $_->(); $val = $_->( 'next' ) if $val <= $last; $low = $val if ! defined $low || $val < $low; } return $last = $low; }; }

Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://441347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found