Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Aplanamiento de cuadrícula 2D de matrices 2D en una sola matriz 2D, en JavaScript (¿funcionalmente?)

Tengo una matriz 2D (cuadrícula) de matrices 2D (fragmentos) para un juego que estoy desarrollando:

 const c1 = [[1, 2], [3, 4]] const c2 = [[5, 6], [7, 8]] const c3 = [[9, 0], [1, 2]] const c4 = [[3, 4], [5, 6]] const grid_of_chunks = [[c1, c2], [c3, c4]];

y quiero reducir/aplanar grid_of_chunks a:

 [[1, 2, 5, 6], [3, 4, 7, 8], [9, 0, 3, 4], [1, 2, 5, 6]]

Pude implementar una solución funcional para esto (en 2 líneas de Clojure), pero estoy luchando por entender cómo traducirlo a JavaScript funcional y cerrar la brecha entre la semántica del map de los dos idiomas (solo mapa JS acepta una matriz, mientras que el map de Clojure acepta muchas colecciones...).

Esto es todo lo que tengo:

 function join_grid_of_chunks(gofc) { const joined_horiz = gofc.map( gofc_row => [].map.apply(gofc_row, [cs => [].concat.apply(cs)]) ); return [].concat.apply(joined_horiz); }

Editar: solución Clojure (que funciona para trozos cuadrados de tamaño uniforme, en una cuadrícula cuadrada de tamaño arbitrario):

 (defn join-grid-of-chunks [gofc] (let [joined (map #(apply map concat %) gofc)] (apply concat joined)))
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esto es lo que tengo:

 const c1 = [[1, 2], [3, 4]]; const c2 = [[5, 6], [7, 8]]; const c3 = [[9, 0], [1, 2]]; const c4 = [[3, 4], [5, 6]]; const grid_of_chunks = [ [c1, c2], [c3, c4] ]; function transform(input) { return input.flatMap(rows => { return rows.reduce((result, chunk) => { chunk.forEach((row, rowIndex) => { result[rowIndex] = result[rowIndex] || []; result[rowIndex].push(...row); }); return result; }, []); }); } console.log(transform(grid_of_chunks));

Debería funcionar para fragmentos NxN y cuadrícula MxM

over 4 years ago · Santiago Trujillo Report

0

Una solución más general que usa flatMap es mapear los índices desde el primer fragmento de cada fila de la cuadrícula.

 function joinGridOfChunks(grid) { return grid.flatMap(row => row[0].map((_, i) => row.flatMap(chunk => chunk[i]))) }

Con una función zip (como la de lodash ), podrías escribirlo un poco más elegantemente como:

 function zip(...arrays) { return arrays[0].map((_, i) => arrays.map(arr => arr[i])) } function joinChunks(chunks) { // Horizontally join an array of chunks eg [[[1,2],[3,4]], [[5,6],[7,8]]] --> [[1,2,5,6],[3,4,7,8]] return zip(...chunks).map(row => row.flat()) } console.log(gridOfChunks.flatMap(joinChunks));

El map zip plus parece estar cerca del map de Clojure con múltiples colecciones. Esto debería funcionar para cualquier forma de fragmentos 2d en una cuadrícula 2d.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!