Skip to content

Instantly share code, notes, and snippets.

@rinster
Last active August 4, 2023 00:53
Show Gist options
  • Save rinster/496027fb5f846c28799bc0ade1b71c95 to your computer and use it in GitHub Desktop.
Save rinster/496027fb5f846c28799bc0ade1b71c95 to your computer and use it in GitHub Desktop.
class DeeplyNestedIterator {
constructor(nestedList) {
this.gen = this.listGenerator(nestedList);
this.nextVal = this.gen.next();
}
hasNext() {
return !this.nextVal.done;
}
getNext() {
const val = this.nextVal.value;
this.nextVal = this.gen.next();
return val;
}
*listGenerator(list) {
for (const el of list) {
if (Array.isArray(el)) yield* this.listGenerator(el);
else yield el;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment