Skip to content

Commit

Permalink
feat: .chain(other) (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Feb 7, 2025
1 parent 5c7fbd2 commit 42f08ca
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions iterpy/arr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from iterpy.iter import Iter


T = TypeVar("T")
S = TypeVar("S")

Expand Down Expand Up @@ -130,6 +131,9 @@ def clone(self) -> Arr[T]:
def zip(self, other: Arr[S]) -> Arr[tuple[T, S]]:
return Arr(zip(self, other))

def chain(self, other: Arr[T]) -> Arr[T]:
return self.lazy().chain(other.lazy()).collect()

############################################################
# Auto-generated overloads for flatten #
# Code for generating the following is in _generate_pyi.py #
Expand Down
3 changes: 3 additions & 0 deletions iterpy/iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def clone(self) -> Iter[T]:
def zip(self, other: Iter[S]) -> Iter[tuple[T, S]]:
return Iter(zip(self, other))

def chain(self, other: Iter[T]) -> Iter[T]:
return Iter((*self, *other))

############################################################
# Auto-generated overloads for flatten #
# Code for generating the following is in _generate_pyi.py #
Expand Down
7 changes: 7 additions & 0 deletions iterpy/test_arr.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def test_looping():
assert i in [1, 2, 3]


def test_chain():
iterator = Arr([1, 2])
iterator2 = Arr([3, 4])
result: list[int] = iterator.chain(iterator2).to_list()
assert result == [1, 2, 3, 4]


@pytest.mark.benchmark()
def test_benchmark_large_flattening():
test_input = Arr(range(100_000)).map(lambda x: Arr([x]))
Expand Down
7 changes: 7 additions & 0 deletions iterpy/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ def test_looping():
assert i in [1, 2, 3]


def test_chain():
iterator = Iter([1, 2])
iterator2 = Iter([3, 4])
result: list[int] = iterator.chain(iterator2).to_list()
assert result == [1, 2, 3, 4]


@pytest.mark.benchmark()
def test_benchmark_large_flattening():
test_input = Iter(range(100_000)).map(lambda x: Iter([x]))
Expand Down
12 changes: 8 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42f08ca

Please sign in to comment.