Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active August 4, 2021 00:17
Show Gist options
  • Save brigand/786f1fe6e4155779f8e40a512763c54e to your computer and use it in GitHub Desktop.
Save brigand/786f1fe6e4155779f8e40a512763c54e to your computer and use it in GitHub Desktop.
class Peekable {
constructor(iter) {
this.iter = iter;
this.deck = [];
}
peek() {
if (!this.deck.length) {
this.deck = [this.iter.next()];
}
return this.deck[0];
}
next() {
if (this.deck.length) {
return this.deck.shift();
} else {
return this.iter.next();
}
}
[Symbol.iterator]() {
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment