From 818e89c7ee0a426ac5f66a4079c70e047627a7f2 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 2 Apr 2026 23:27:55 -0300 Subject: content: generateDividedTrackList: context groups --- src/common-util/sugar.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/common-util/sugar.js') 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 -- cgit 1.3.0-6-gf8a5