Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
¿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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda