Учитывая, разбивая массив на p частей размером ≥ 1 каждая:
my @a = 'A'..'F';
# p = 1
my @p1 = [@a];
# ["A" .. "F"]
# p = 2
my @p2;
for my $x (0..@a-2) {
push @p2, [
[@a[0..$x]],
[@a[$x+1..@a-1]],
];
}
# [["A"], ["B" .. "F"]],
# [["A", "B"], ["C" .. "F"]],
# [["A", "B", "C"], ["D", "E", "F"]],
# [["A" .. "D"], ["E", "F"]],
# [["A" .. "E"], ["F"]],
# p = 3
my @p3;
for my $x (0..@a-3) {
for my $y ($x+1..@a-2) {
push @p3, [
[@a[0..$x]],
[@a[$x+1..$y]],
[@a[$y+1..@a-1]],
];
}
}
# [["A"], ["B"], ["C" .. "F"]],
# [["A"], ["B", "C"], ["D", "E", "F"]],
# [["A"], ["B", "C", "D"], ["E", "F"]],
# [["A"], ["B" .. "E"], ["F"]],
# [["A", "B"], ["C"], ["D", "E", "F"]],
# [["A", "B"], ["C", "D"], ["E", "F"]],
# [["A", "B"], ["C", "D", "E"], ["F"]],
# [["A", "B", "C"], ["D"], ["E", "F"]],
# [["A", "B", "C"], ["D", "E"], ["F"]],
# [["A" .. "D"], ["E"], ["F"]],
# p = 4
my @p4;
for my $x (0..@a-4) {
for my $y ($x+1..@a-3) {
for my $z ($y+1..@a-2) {
push @p4, [
[@a[0..$x]],
[@a[$x+1..$y]],
[@a[$y+1..$z]],
[@a[$z+1..@a-1]],
];
}
}
}
# [["A"], ["B"], ["C"], ["D", "E", "F"]],
# [["A"], ["B"], ["C", "D"], ["E", "F"]],
# [["A"], ["B"], ["C", "D", "E"], ["F"]],
# [["A"], ["B", "C"], ["D"], ["E", "F"]],
# [["A"], ["B", "C"], ["D", "E"], ["F"]],
# [["A"], ["B", "C", "D"], ["E"], ["F"]],
# [["A", "B"], ["C"], ["D"], ["E", "F"]],
# [["A", "B"], ["C"], ["D", "E"], ["F"]],
# [["A", "B"], ["C", "D"], ["E"], ["F"]],
# [["A", "B", "C"], ["D"], ["E"], ["F"]],
Как мне абстрагироваться от увеличивающегося числа вложенных циклов, чтобы превратить его в суб slices(Int $p, Array @a)
?Я думаю, мне нужен какой-то более высокий порядок foreach
.