about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-09-02 17:06:41 -0400
committerelioat <elioat@tilde.institute>2024-09-02 17:06:41 -0400
commitbfa68403b33104e80337b87c958492219955210e (patch)
treec240ab1632641f99c1afadde5d79a1b415050440
parentecc5db886ecbcc0aade755dc84b4b801a6314bc6 (diff)
downloadtour-bfa68403b33104e80337b87c958492219955210e.tar.gz
*
-rw-r--r--js/reshape.js57
1 files changed, 28 insertions, 29 deletions
diff --git a/js/reshape.js b/js/reshape.js
index cf74f14..f45b76f 100644
--- a/js/reshape.js
+++ b/js/reshape.js
@@ -1,35 +1,34 @@
-// more details, <https://eli.li/reshape-in-javascript-and-apl
+// more details, <https://eli.li/reshape-in-javascript-and-apl>
 
 function ravel(array) {
-    if (!Array.isArray(array)) return [array];
-    return array.reduce((acc, val) => acc.concat(ravel(val)), []);
+  if (!Array.isArray(array)) return [array];
+  return array.flatMap(val => ravel(val));
 }
 
 function reshape(shape, array) {
-    const totalSize = shape.reduce((acc, val) => acc * val, 1);
-    const ravelledArray = ravel(array);
-    const filledArray = [];
-  
-    for (let i = 0; i < totalSize; i++) {
-      filledArray.push(ravelledArray[i % ravelledArray.length]);
-    }
-  
-    function constructArray(shape, data, offset = 0) {
-      if (shape.length === 1) {
-        return data.slice(offset, offset + shape[0]);
-      }
-  
-      const size = shape[0];
-      const subShape = shape.slice(1);
-      const subArraySize = subShape.reduce((acc, val) => acc * val, 1);
-  
-      const result = [];
-      for (let i = 0; i < size; i++) {
-        result.push(constructArray(subShape, data, offset + i * subArraySize));
-      }
-      
-      return result;
+  const totalSize = shape.reduce((acc, val) => acc * val, 1);
+  const ravelledArray = ravel(array);
+  const filledArray = Array.from({ length: totalSize }, (_, i) => ravelledArray[i % ravelledArray.length]);
+
+  // This function builds a nested array based on the given shape and data.
+  // It recursively constructs the nested array by slicing the data array and creating subarrays.
+  // The offset parameter is used to keep track of the current position in the data array.
+  function buildNestedArray(shape, data, offset = 0) {
+    // If the shape has only one element, return a slice of the data array with the specified size.
+    if (shape.length === 1) {
+      return data.slice(offset, offset + shape[0]);
     }
-  
-    return constructArray(shape, filledArray);
-}
\ No newline at end of file
+
+    // Extract the first element of the shape array as the size of the current dimension.
+    // The remaining elements form the subshape.
+    const [size, ...subShape] = shape;
+
+    // Calculate the size of the subarray for the next dimension.
+    const subArraySize = subShape.reduce((acc, val) => acc * val, 1);
+
+    // Create an array of size 'size' and recursively call buildNestedArray for each element.
+    return Array.from({ length: size }, (_, i) => buildNestedArray(subShape, data, offset + i * subArraySize));
+  }
+
+  return buildNestedArray(shape, filledArray);
+}