All,
I
also have successfully written my first perl 6 code! It was a pretty straight forward port of
Generating powerset with progressive ordering. The only thing
Pugs didn't have that I wanted was @array.end which I am in the process of writing a test for. You see
autrijus and the Pugs team has this deal where they will trade working code for a proper test.
use v6;
sub iter_powerset ( *@factor ) returns Ref {
my @subset = (undef) xx @factor.end;
my ($pos, $mode) = (-1, 1);
my $return = sub { list @factor[ grep { defined $_ } @subset ] };
my %dispatch = (
1 => sub {
++$pos;
@subset[ $pos ] = $pos;
++$mode if $pos == @factor.end;
$return();
},
2 => sub {
@subset[ $pos - 1 ] = undef;
++$mode;
$return();
},
3 => sub {
@subset[ $pos-- ] = undef;
while ( $pos >= 0 ) {
last if defined @subset[ $pos ];
--$pos;
}
@subset[ $pos++ ] = undef;
return () if ! $pos;
@subset[ $pos ] = $pos;
$mode = 1;
$return();
},
);
return sub { %dispatch{ $mode }() };
}
my $next = iter_powerset( 1..5 );
my @combo;
while ( @combo = $next() ) {
@combo.say;
}
Incidently, the @array.end feature was waiting for me as soon as I checked in the test.