Skip to content

Commit

Permalink
SkipIterator: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Jan 2, 2025
1 parent 6f4ce4e commit 1df84dc
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions streamable/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,16 @@ def __init__(self, iterator: Iterator[T], count: int) -> None:
validate_count(count)
self.iterator = iterator
self.count = count
self._skipped = 0
self._n_skipped = 0
self._done_skipping = False

def __next__(self):
while self._skipped < self.count:
next(self.iterator)
# do not count exceptions as skipped elements
self._skipped += 1
if not self._done_skipping:
while self._n_skipped < self.count:
next(self.iterator)
# do not count exceptions as skipped elements
self._n_skipped += 1
self._done_skipping = True
return next(self.iterator)


Expand Down

0 comments on commit 1df84dc

Please sign in to comment.