I'm attempting to port the function found in this answer from Python to ES6 (minus the sorting, which I don't need), but can't reproduce the same output.
Instead of returning only the partitions of a given length, as in the Python version, it is currently returning all of the partitions up to and including that length.
In the below example, the desired output is arrays of length 3 to be returned only.
Have I misunderstood some aspect of the generator function?
const kPartitions = (seq, k) => {
const n = seq.length
var groups = []
function* generatePartitions(i) {
if (i >= n) {
yield groups.map(group => [...group])
}
else {
if (n - 1 > k - groups.length) {
for (let group of groups) {
group.push(seq[i])
yield* generatePartitions(i + 1)
group.pop()
}
}
if (groups.length < k) {
groups.push([seq[i]])
yield* generatePartitions(i + 1)
groups.pop()
}
}
}
return generatePartitions(0)
}
for (var partitions of kPartitions(['A', 'B', 'C', 'D'], 3)) {
console.log(partitions)
}
Also as a bin.