sub combo (Int $by is copy, @list is copy) returns Ref { my @position = 0 .. ($by - 2), $by - 2; my @stop = @list.elems - $by .. @list.end; my $done = undef; gather { until $done { my $cur = @position.end; while ++@position[$cur] > @stop[$cur] { --$cur; @position[$cur]++; next if @position[$cur] > @stop[$cur]; my $new_pos = @position[$cur]; @position[$cur .. @position.end] = $new_pos .. ($new_pos + $by); last; } $done = 1 if @position[0] == @stop[0]; take [ @list[@position] ]; } } }