Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: cf222bccab
Fetching contributors…

Cannot retrieve contributors at this time

41 lines (38 sloc) 0.955 kB
use v6;
sub iter_powerset ( *@factor ) {
my $end = @factor.elems - 1;
my @subset = (undef) xx $end;
my ($pos, $mode) = (-1, 1);
my $return = { list @factor[ grep { defined $_ }, @subset ] };
my %dispatch = (
1 => {
++$pos;
@subset[ $pos ] = $pos;
++$mode if $pos == $end;
$return();
},
2 => {
@subset[ $pos - 1 ] = undef;
++$mode;
$return();
},
3 => {
@subset[ $pos-- ] = undef;
while ( $pos >= 0 ) {
last if defined @subset[ $pos ];
--$pos;
}
@subset[ $pos++ ] = undef;
last if !$pos;
@subset[ $pos ] = $pos;
$mode = 1;
$return();
},
);
return { %dispatch{ $mode }() };
}
my $next = iter_powerset( 1..5 );
my @combo;
while ( @combo = $next() ) {
@combo.say;
}
Jump to Line
Something went wrong with that request. Please try again.