« get me outta code hell

content: generateDividedTrackList: context groups - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util/sugar.js
diff options
context:
space:
mode:
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
commit818e89c7ee0a426ac5f66a4079c70e047627a7f2 (patch)
tree489d6651767e7b7214324e8dfbc47fed1d2deda6 /src/common-util/sugar.js
parentb250e91a74b244cc5decd79f5604cfb8a811421a (diff)
content: generateDividedTrackList: context groups
Diffstat (limited to 'src/common-util/sugar.js')
-rw-r--r--src/common-util/sugar.js32
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