go ahead... be a heretic PerlMonks

### Comment on

 Need Help??
All,
In this node, tall_man asked how to generate the Hamming Sequence lazily in Perl5. I didn't understand the Haskell code and misunderstood the english explanation.

"how do you generate the series of numbers composed of a given list of prime factors, where each can be used an unlimited number of times?"

I interpreted that to mean all multiples of all prime factors provided minus duplicates were valid. My mistake was exacerbated by the fact the sample series provided fit my interpretation. I took my licks after posting an incorrect solution.

I was happy to take the downvotes and be wrong since the mistake was just as interesting to me. My challenge then is to produce R as shown below for any set of given factors lazily:

```Given:  Factors 2, 3, 5
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, ...
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, ...
5, 10, 15, 20, 25, 30, ...

R = 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 2
+6, 27, 28, 30, ...
You can see my cleaned up solution below:
```#!/usr/bin/perl
use strict;
use warnings;

use constant HEAD =>  0;
use constant TAIL => -1;

my \$end = shift || 10;
my @num = @ARGV ? @ARGV : (2, 3, 5);
my \$next = merge_multiple( \@num );

print \$next->(), "\n" for 1 .. \$end;

sub merge_multiple {
my \$list = shift;
return () if ! \$list || ref \$list ne 'ARRAY';

my \$n = 1;
my \$h = \$list->[TAIL];
my (@pool, @stream);

return sub {
return shift @pool if @pool;

for ( 0 .. \$#\$list ) {
my \$mult = \$list->[ \$_ ];
my \$beg = \$h * (\$n - 1) / \$mult + 1;
my \$end = \$h * \$n / \$mult;
\$stream[ \$_ ] = [ map { \$mult * \$_ } \$beg .. \$end ];
}
++\$n;

@pool = merge( \@stream );
return shift @pool;
}
}

sub merge {
my \$stream = shift;
my \$end = \$#\$stream;

my @merged;
while ( 1 ) {
my \$low;
for ( 0 .. \$end ) {
my \$val = \$stream->[ \$_ ][HEAD];
next if ! defined \$val;
\$low = \$_ if ! defined \$low || \$val < \$stream->[ \$low ][HE
}
last if ! defined \$low;
my \$num = shift @{ \$stream->[ \$low ] };
next if defined \$merged[ TAIL ] && \$merged[ TAIL ] == \$num;
push @merged, \$num;
}
return @merged;
}

Cheers - L~R

In reply to Challenge: Another Infinite Lazy List by Limbic~Region

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!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• 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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
 [tye]: 1) The python stack traces show you the names of variables, not the values of variables. Almost never informative. [LanX]: do you have a py job now? [tye]: Right now I have errors trying to fetch stuff from graphite. It says 'apps not ready, check startup logs'. I eventually find the startup logs and see 'no apps found'. Looking for fixes to this problem... [LanX]: no idea ... ask py monks! ;-p [tye]: it turns out that this can be caused by almost anything going wrong. If you want to know what caused the problem, you have to try to load each app by-hand to see what problems it is having, because none of those will be logged. [tye]: No, I write more Perl than py at work. But I have to deal with plenty of py things. [tye]: py monks would just be offended. [Corion]: ;) [Corion]: Hi tye btw ;) [Corion]: I found plenty of not chatty enough logs with Perl too ... I'm slowly coming to appreciate Log::Log4perl resp. our homegrown alternative

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2017-09-21 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (252 votes). Check out past polls.

Notices?