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

294
Views
Lista doblemente enlazada inmutable, puramente funcional en JavaScript

En JavaScript, es trivial crear un par de nodos que se referencian entre sí en un bucle infinito:

 var node = item => next => previous => { return { item: item, previous: previous, next: next }; }; var z = { a: node('a')(() => zb)(() => zb), b: node('b')(() => za)(() => za) };

Tome za o zb y podrá llamar a next() y previous() infinitamente.

¿Es posible instanciar, por ejemplo, para un carrusel con cualquier tamaño que se pueda desplazar en cualquier dirección, una lista circular enlazada que puede tener un número arbitrario de elementos cuando se instancia?

He leído algunas cosas de Haskell Wiki sobre "Tying the Knot" y encontré ejemplos en Scala, pero no estoy seguro de cómo hacer que funcionen en JavaScript.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar el mismo principio que usó para z . En lugar de crear un objeto con dos propiedades a y b , cree una matriz, que tendrá índices de matriz en su lugar.

 var Node = item => next => previous => ({ item: item, previous: previous, next: next }); var arr = (length => Array.from({length}, (_, i) => Node(i)(() => arr[(i + 1) % length]) (() => arr[(i + length - 1) % length]) ) )(4); // IIFE - we want 4 nodes in circular list // Demo iterating the 4 nodes var node = arr[0]; // Get one of the nodes setInterval(() => console.log((node = node.next()).item), 500);

recursividad

En lugar de almacenar los nodos en una matriz, podrían almacenarse en contextos de ejecución recursiva.

Por ejemplo:

 var head = (function CircularList(previous, item, ...items) { if (!items.length) return { item, next: () => head, previous }; var rest = CircularList(() => current, ...items); // Recursion var current = { ...rest, previous }; return { ...rest, item, next: () => current }; })(() => head, 1, 2, 3, 4); // Example: list of four values // Demo iterations console.log(head.item); for (let node = head.next(); node != head; node = node.next()) { console.log(node.item); } console.log("----"); console.log(head.item); for (let node = head.previous(); node != head; node = node.previous()) { console.log(node.item); } console.log("----");

about 4 years ago · Juan Pablo Isaza 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!