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

198
Views
¿Los generadores de JavaScript son copiables/clonables?

Estoy escribiendo pruebas para una función generadora que recorre un árbol. La next función acepta un valor booleano para determinar si debe continuar explorando esa rama del árbol. Me gustaría poder probarlo de la siguiente manera:

 let tree_walker = make_tree_walking_generator(some_arguments) let result = tree_walker.next() test(result.value, "0", "Test it returns root element") // I assume this is not possible yet / ever? let tree_walker_clone = clone_generator_and_state(tree_walker) // <-- result = tree_walker.next(true) // true === first element was consumed (ie it was all that was needed) test(result.value, undefined, "Test it returns undefined if last element was consumed") test(result.done, true) // should be finished // Restore the original state captured in the clone tree_walker = tree_walker_clone result = tree_walker.next(false) // false === first element was not consumed test(result.value, "0->1", "Test it returns the next element if the last element was not consumed") test(result.done, false) // should not be finished

Esto es importante porque, de lo contrario, debe volver a llamar al generador con los mismos argumentos, por ejemplo:

 // get the same starting generator again tree_walker = make_tree_walking_generator(some_arguments) // Bring it back up to the same penultimate position (n-1) result = tree_walker.next() // Continue testing from this point. The problem is the previous // call is made twice and for a deep tree this might be many 10s of // calls with complex arguments. result = tree_walker.next(false) // false === first element was not consumed test(result.value, "0->1", "Test it returns the next element if the last element was not consumed") test(result.done, false) // should not be finished

Una alternativa a alguna función de clonación sería la capacidad de rebobinar un generador. Una segunda alternativa es escribir un contenedor que registre todos los argumentos y ( suponiendo que el generador sea puro ) pueda reproducir los últimos n-1 argumentos para obtener un generador en el estado anterior.

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