Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I had my own idea of how to do it, and I came up with restartable iterators:
#!/usr/bin/perl use strict; use warnings; sub restartable_iter { my ($start, $end) = @_; sub { $start = shift if @_; return if $start > $end; $start++; } } sub choose_m_of_n_iter { my ($m, $n) = @_; my @iter; for my $i (0..($m-1)) { push @iter, restartable_iter($i, $n-$m+$i); } join_iter(@iter); } sub join_iter { my $it = shift; while ( my $tmp = shift ) { $it = append_iter( $it, $tmp ); } $it; } sub append_iter { my ($it1, $it2) = @_; my (@ret1, @ret2) = $it1->(); sub { return @ret1, @ret2 if @ret2 = $it2->(); return unless @ret1 = $it1->(); return @ret1, $it2->($ret1[-1]+1); } } my $iter = choose_m_of_n_iter(4, 10); while (my @arr = $iter->()) { print "@arr\n"; }

In the append iterator, when the second iterator is exhausted, it causes the first iterator to iterate, and then restarts the second iterator at the appropriate starting point.

Update:Unlike the other solutions, the solution above returns an array of indices instead of a set of elements from a list, but that's easy to adjust:

sub choose_n { my $n = shift; my @set = @_; my $iter = choose_m_of_n_iter($n, scalar(@set)); sub { @set[$iter->()]; } }

Update: Simplified code. Which may or may not be a good thing :-)


In reply to Re: Recursively-generated Iterators by runrig
in thread Recursively-generated Iterators by Roy Johnson

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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 exploiting the Monastery: (10)
    As of 2015-07-31 19:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (280 votes), past polls