Considering,

Updated. Cleaner version, without (too much) poking inside other modules guts. + I always wanted to use the PadWalker for something real.

use strict; use warnings; use feature 'say'; use PadWalker qw/ closed_over set_closed_over /; use Algorithm::Combinatorics qw/ combinations_with_repetition /; my @data = qw( a b c ); my $state; { say "\tDay one, hard work ahead..."; my $iter = combinations_with_repetition( \@data, 3 ); say @{ $iter-> next } for 1 .. 5; say "\tEnough work for one day!"; $state = closed_over( $iter ); } { say "\tDay 2, back to work..."; my $iter = combinations_with_repetition( \@data, 3 ); $iter-> next; # init set_closed_over( $iter, $state ); say "\tDeadline! Exhaust the iterator!"; while ( my $c = $iter-> next ) { say @$c } } __END__ >perl comb.pl Day one, hard work ahead... aaa aab aac abb abc Enough work for one day! Day 2, back to work... Deadline! Exhaust the iterator! acc bbb bbc bcc ccc

We need the line with "init" comment because first call to iterator is special.

a not very clean hack would be:

use strict; use warnings; use feature 'say'; use Algorithm::Combinatorics qw( combinations_with_repetition ); package Algorithm::Combinatorics { no warnings 'redefine'; sub main::combinations_with_repetition { my ($data, $k, $ref) = @_; __check_params($data, $k); return __contextualize(__null_iter()) if $k < 0; return __contextualize(__once_iter()) if $k == 0; my @indices = $ref ? @$ref : (0) x $k; my $iter = Algorithm::Combinatorics::Iterator->new(sub { __next_combination_with_repetition(\@indices, @$data-1) == -1 +? undef : [ @{$data}[@indices] ]; }, [ @{$data}[@indices] ]); my $x = __contextualize($iter); # Note: scalar context forced return [ $x, \@indices ] # + interface changed } } my @data = qw( a b c ); my ( $iter, $secret ) = @{ combinations_with_repetition( \@data, 3 )}; say @{ $iter-> next } for 1..5; my ( $iter_2 ) = @{ combinations_with_repetition( \@data, 3, $secret )}; say '**********'; $iter_2-> next; # skip 1 while ( my $p = $iter_2-> next ) { say @$p } __END__ aaa aab aac abb abc ********** acc bbb bbc bcc ccc

In reply to Re: Looking for combinatorics with state (Updated) by vr
in thread Looking for combinatorics with state by Anonymous Monk

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, details, 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, summary, 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.