28 lines
621 B
Python
28 lines
621 B
Python
|
|
from collections import deque
|
||
|
|
from typing import Generator, TypeVar
|
||
|
|
|
||
|
|
T1 = TypeVar("T1")
|
||
|
|
T2 = TypeVar("T2")
|
||
|
|
T3 = TypeVar("T3")
|
||
|
|
|
||
|
|
|
||
|
|
def iterator_with_checks(
|
||
|
|
generator: Generator[T1, T2, T3],
|
||
|
|
) -> Generator[tuple[T1, bool], T2, T3]:
|
||
|
|
|
||
|
|
# Here we can ignore to catch stop iteration
|
||
|
|
# we will propagate it
|
||
|
|
last_element = next(generator)
|
||
|
|
|
||
|
|
while True:
|
||
|
|
|
||
|
|
RETURN_ELEMENT = last_element
|
||
|
|
try:
|
||
|
|
element = next(generator)
|
||
|
|
last_element = element
|
||
|
|
yield (RETURN_ELEMENT, False)
|
||
|
|
|
||
|
|
except StopIteration:
|
||
|
|
yield (RETURN_ELEMENT, True)
|
||
|
|
break
|