Skip to content

Commit

Permalink
fix(#137): implement lazy flattening (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Mar 7, 2024
2 parents 52fe4d5 + 7c5674e commit 3023cf0
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions iterpy/iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import copy
import multiprocessing
from collections import defaultdict
from collections.abc import Callable, Iterable, Iterator, Sequence
from functools import reduce
from itertools import islice
from typing import Generic, TypeVar
from typing import TYPE_CHECKING, Any, Generator, Generic, TypeVar

if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Iterator

T = TypeVar("T")
S = TypeVar("S")
Expand Down Expand Up @@ -97,16 +99,22 @@ def groupby(self, func: Callable[[T], str]) -> Iter[tuple[str, list[T]]]:
return Iter(tuples)

def flatten(self) -> Iter[T]:
values: list[T] = []
for i in self._iterator:
if isinstance(i, Sequence) and not isinstance(i, str):
values.extend(i) # type: ignore
elif isinstance(i, Iter):
values.extend(i.to_list()) # type: ignore
depth = 1

def walk(node: Any, level: int) -> Generator[T, None, None]:
if (level > depth) or isinstance(node, str):
yield node # type: ignore
return
try:
tree = iter(node)
except TypeError:
yield node
return
else:
values.append(i)
for child in tree:
yield from walk(child, level + 1)

return Iter(values)
return Iter(walk(self, level=0)) # type: ignore

def take(self, n: int = 1) -> Iter[T]:
return Iter(islice(self._iter, n))
Expand Down Expand Up @@ -141,7 +149,7 @@ def find(self, func: Callable[[T], bool]) -> T | None:
return value
return None

def copy(self) -> Iter[T]:
def clone(self) -> Iter[T]:
return copy.deepcopy(self)

def zip(self, other: Iter[S]) -> Iter[tuple[T, S]]:
Expand Down

0 comments on commit 3023cf0

Please sign in to comment.