| Total Complexity | 9 |
| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import typing |
||
| 2 | |||
| 3 | T = typing.TypeVar("T") |
||
| 4 | |||
| 5 | |||
| 6 | class Action: |
||
| 7 | def handle(self, to_process: typing.List[T]): |
||
| 8 | raise NotImplementedError |
||
| 9 | |||
| 10 | |||
| 11 | class Yield(Action): |
||
| 12 | def __init__(self, item) -> None: |
||
| 13 | self.item = item |
||
| 14 | |||
| 15 | def handle(self, _to_process): |
||
| 16 | yield self.item |
||
| 17 | |||
| 18 | |||
| 19 | class Extend(Action): |
||
| 20 | def __init__(self, iterable) -> None: |
||
| 21 | self.iterable = iterable |
||
| 22 | |||
| 23 | def handle(self, to_process): |
||
| 24 | to_process.extend(self.iterable) |
||
| 25 | yield from () |
||
| 26 | |||
| 27 | |||
| 28 | def handle(action: typing.Optional[Action], to_process): |
||
| 29 | if action is not None: |
||
| 30 | yield from action.handle(to_process) |
||
| 31 | |||
| 32 | |||
| 33 | def stack_iter(first: T, process: typing.Callable[[T], typing.Optional[Action]]): |
||
| 34 | to_process = [first] |
||
| 35 | while to_process: |
||
| 36 | yield from handle(process(to_process.pop()), to_process) |
||
| 37 |