root/examples/progressive_powerset.pl

Revision 11293, 1.0 kB (checked in by Darren_Duncan, 2 years ago)

replaced all 'use v6;' lines with 'use v6-alpha;' in 330 files (examples/, ext/, t/, t_disabled/) ... more remain to do

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1use v6-alpha;
2
3sub iter_powerset ( *@factor ) returns Ref {
4    my $end = @factor.elems - 1;
5    my @subset = (undef) xx $end;
6    my ($pos, $mode) = (-1, 1);
7    my $return = { list @factor[ grep { defined $_ }, @subset ] };
8    my %dispatch = (
9        1 => {
10            ++$pos;
11            @subset[ $pos ] = $pos;
12            ++$mode if $pos == $end;
13            $return();
14        },
15        2 => {
16            @subset[ $pos - 1 ] = undef;
17            ++$mode;
18            $return();
19        },
20        3 => {
21            @subset[ $pos-- ] = undef;
22            while ( $pos >= 0 ) {
23                last if defined @subset[ $pos ];
24                --$pos;
25            }
26            @subset[ $pos++ ] = undef;
27            last if !$pos;
28            @subset[ $pos ] = $pos;
29            $mode = 1;
30            $return();
31        },
32    );
33    return { %dispatch{ $mode }() };
34}
35
36my $next = iter_powerset( 1..5 );
37my @combo;
38while ( @combo = $next() ) {
39    @combo.say;
40}
Note: See TracBrowser for help on using the browser.