Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here is a lazy-list example of merge, with one of its arguments being a finite list, and the other an infinite list. I may not get around to figuring out the Hamming generator, but I think the techniques I've employed here tell you how it can be done.
use strict; use warnings; # Merge takes two iterators, which can be called # with no arguments, in which case they iterate, returning # their next value; or with a string 'peek', which will # yield the next value without effectively shifting it. # Exhausted iterators return undef on all subsequent calls # # Merge itself is an iterator, returning the next element in # the merged series, plus a continuation coderef sub merge { my ($a, $b) = @_; my ($car_a, $car_b) = ($a->('peek'), $b->('peek')); # Base case: if one of them is empty, return the other defined $car_a or return ($b->(), sub {merge($a, $b)}); defined $car_b or return ($a->(), sub {merge($a, $b)}); # Pull off lesser (both if equal) first element(s) my $low_car; if ($car_a <= $car_b) { $low_car = $a->() } if ($car_b <= $car_a) { $low_car = $b->() } return ($low_car, sub {merge($a, $b)} ); } my $I1 = do { my @arr = (1..10); sub { if (@arr == 0) { return undef; } elsif (@_) { # should be peek, but any other arg works (undocu +mented) return $arr[0]; } else { return shift @arr; } } }; my $I2 = do { my $i = 2; sub { if (@_) { return $i; } else { my $i_was = $i; $i += 2; return $i_was; } } }; my $iterations_to_print = 30; for ( my ($elem, $cont) = merge($I1, $I2); $iterations_to_print-- > 0; ($elem, $cont) = $cont->() ) { print "<$elem>\n"; }

Caution: Contents may have been coded under pressure.

In reply to Re: Hamming Sequences and Lazy Lists by Roy Johnson
in thread Hamming Sequences and Lazy Lists by tall_man

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (7)
    As of 2014-09-21 18:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (174 votes), past polls