diff options
| author | (quasar) nebula <qznebula@protonmail.com> | 2026-04-02 23:27:55 -0300 |
|---|---|---|
| committer | (quasar) nebula <qznebula@protonmail.com> | 2026-04-02 23:27:55 -0300 |
| commit | 818e89c7ee0a426ac5f66a4079c70e047627a7f2 (patch) | |
| tree | 489d6651767e7b7214324e8dfbc47fed1d2deda6 /src/common-util/sugar.js | |
| parent | b250e91a74b244cc5decd79f5604cfb8a811421a (diff) | |
content: generateDividedTrackList: context groups
Diffstat (limited to 'src/common-util/sugar.js')
| -rw-r--r-- | src/common-util/sugar.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index c988156c..26f33c20 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -229,6 +229,38 @@ export const mapInPlace = (array, fn) => export const unique = (arr) => Array.from(new Set(arr)); +export function* permutations(array) { + switch (array.length) { + case 0: return; + case 1: yield array; return; + default: { + const behind = []; + const ahead = array.slice(); + while (ahead.length) { + const here = ahead.shift(); + + yield* + permutations([...behind, ...ahead]) + .map(slice => [here, ...slice]); + + behind.push(here); + } + } + } +} + +export function* runs(array) { + switch (array.length) { + case 0: return; + case 1: yield array; return; + default: { + yield* runs(array.slice(1)).map(run => [array[0], ...run]); + yield [array[0]]; + yield* runs(array.slice(1)); + } + } +} + export const compareArrays = (arr1, arr2, {checkOrder = true} = {}) => arr1.length === arr2.length && (checkOrder |