Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Challenge: Another Infinite Lazy List

by Limbic~Region (Chancellor)
on Mar 22, 2005 at 00:19 UTC ( #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


Comment on Re: Challenge: Another Infinite Lazy List
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-30 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls