in reply to Looking for combinatorics with state

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

Replies are listed 'Best First'.
Re^2: Looking for combinatorics with state (updated)
by AnomalousMonk (Archbishop) on May 26, 2018 at 14:56 UTC
    package Algorithm::Combinatorics { ... }

    NB: The   package NAMESPACE BLOCK syntax was not available prior to Perl version 5.14.

    Update: It occurs to me that I should mention what is available if the aforementioned syntax is not. The following is essentially the same:
        { package NAMESPACE;  statement;  statement;  ...;  ... }
    (Indeed, I think it's exactly equivalent, but I'm not quite sure about this :)


    Give a man a fish:  <%-{-{-{-<

      Yes, it is equivalent. When written on the same line as the opening curly bracket
      { package My::Package; ... }
      you can see the new feature only moved the bracket few words forward and removed a semicolon.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,