Skip to content

Instantly share code, notes, and snippets.

@rinster
Last active August 3, 2023 19:57
Show Gist options
  • Save rinster/65b766f0144321436e3aede4a8afffff to your computer and use it in GitHub Desktop.
Save rinster/65b766f0144321436e3aede4a8afffff to your computer and use it in GitHub Desktop.
class DeepNestedIterator {
constructor(nestedList) {
this.data = []
// Recursively precomputes all data at
// the moment the class is instantiated
this.flatten(nestedList)
};
flatten(list) {
for (let el of list)
if (Number.isInteger(el)) this.data.push(el)
else this.flatten(el)
};
hasNext() { return this.data.length };
next() { return this.data.shift() };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment