Skip to content

Commit

Permalink
feat(#98): add unique
Browse files Browse the repository at this point in the history
Fixes #98
  • Loading branch information
MartinBernstorff committed Feb 10, 2024
1 parent 26bc481 commit 7cae674
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
27 changes: 24 additions & 3 deletions iterpy/iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,29 @@ def flatten(self) -> "Iter[T]":

return Iter(values)

def head(self, n: int = 1) -> "Iter[T]":
def take(self, n: int = 1) -> "Iter[T]":
return Iter(self._nonconsumable_iterable[0:n])

def tail(self, n: int = 1) -> "Iter[T]":
return Iter(self._nonconsumable_iterable[n - 1 :])
def rev(self) -> "Iter[T]":
return Iter(reversed(self._nonconsumable_iterable))

def any(self, func: Callable[[T], bool]) -> bool:
return any(func(i) for i in self._iterator)

def all(self, func: Callable[[T], bool]) -> bool:
return all(func(i) for i in self._iterator)

def unique(self) -> "Iter[T]":
return Iter(set(self._iterator))

def unique_by(self, func: Callable[[T], S]) -> "Iter[T]":
seen: set[S] = set()
values: list[T] = []

for value in self._iterator:
key = func(value)
if key not in seen:
seen.add(key)
values.append(value)

return Iter(values)
8 changes: 6 additions & 2 deletions iterpy/iter.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ class Iter(Generic[T]):
def pmap(self, func: Callable[[T], S]) -> Iter[S]: ...
def filter(self, func: Callable[[T], bool]) -> Iter[T]: ...
def reduce(self, func: Callable[[T, T], T]) -> T: ...
def head(self, n: int) -> Iter[T]: ...
def tail(self, n: int) -> Iter[T]: ...
def take(self, n: int) -> Iter[T]: ...
def rev(self) -> Iter[T]: ...
def any(self, func: Callable[[T], bool]) -> bool: ...
def all(self, func: Callable[[T], bool]) -> bool: ...
def unique(self) -> Iter[T]: ...
def unique_by(self, func: Callable[[T], U]) -> Iter[T]: ...
def groupby(
self, func: Callable[[T], str]
) -> Iter[tuple[str, list[T]]]: ...
Expand Down
33 changes: 29 additions & 4 deletions iterpy/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,39 @@ def test_statefulness():
assert test_iterator.to_list() == [1, 2, 3]


def test_head():
def test_take():
test_iterator = Iter([1, 2, 3])
assert test_iterator.head(2) == Iter([1, 2])
assert test_iterator.take(2) == Iter([1, 2])


def test_tail():
def test_rev():
test_iterator = Iter([1, 2, 3])
assert test_iterator.tail(2) == Iter([2, 3])
assert test_iterator.rev().to_list() == [3, 2, 1]


def test_any():
test_iterator = Iter([1, 2, 3])
assert test_iterator.any(lambda x: x == 2) is True
assert test_iterator.any(lambda x: x == 4) is False


def test_all():
test_iterator = Iter([1, 2, 3])
assert test_iterator.all(lambda x: x < 4) is True
assert test_iterator.all(lambda x: x < 3) is False


def test_unique():
test_iterator = Iter([1, 2, 2, 3])
assert test_iterator.unique().to_list() == [1, 2, 3]


def test_unique_by():
test_iterator = Iter([1, 2, 2, 3])
assert test_iterator.unique_by(lambda x: x % 2).to_list() == [
1,
2,
]


def test_flatten():
Expand Down

0 comments on commit 7cae674

Please sign in to comment.