Last active
August 4, 2023 00:53
-
-
Save rinster/496027fb5f846c28799bc0ade1b71c95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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