Skip to content

Commit

Permalink
Rewrite reduceRight
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 13, 2020
1 parent 6de1268 commit c00fe99
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions rules/utils/cartesian-product-samples.js
Expand Up @@ -14,12 +14,16 @@ module.exports = (combinations, length = Infinity) => {

const samples = Array.from({length: Math.min(total, length)}, (_, sampleIndex) => {
let indexRemaining = sampleIndex;
return combinations.reduceRight((combination, items) => {
const combination = [];
for (let i = combinations.length - 1; i >= 0; i--) {
const items = combinations[i];
const {length} = items;
const index = indexRemaining % length;
indexRemaining = (indexRemaining - index) / length;
return [items[index], ...combination];
}, []);
combination.unshift(items[index]);
}

return combination;
});

return {
Expand Down

0 comments on commit c00fe99

Please sign in to comment.