Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip with next #217

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

## Release 1.6

- Added [parametrize](https://pypi.org/project/parametrize/) for parameterized unit tests
- Fixed tests failing (to find test files) when running from the IDE or the terminal when not in the right directory
- Added Python version to GitHub Action workflow job steps and set Black to show required formatting changes
- Upgraded pre-commit hooks (pre-commit-hooks to `v5.0.0` and ruff-pre-commit to `v0.6.0`)
- Added [run-test.sh](run-tests.sh) script that runs all checks on code
- Added support for Python 3.12 and 3.13 by upgrading Pylint and disabling/fixing Pylint errors
- Corrected and improved language consistency in [readme](README.md) and `CHANGELOG.md`

### New Features

- Implemented `zip_with_next` function

## Release 1.5

## Release 1.4
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ complete documentation reference
| `tails()` | Returns consecutive tails of sequence | transformation |
| `zip(other)` | Zips the sequence with `other` | transformation |
| `zip_with_index(start=0)` | Zips the sequence with the index starting at `start` on the right side | transformation |
| `zip_with_next()` | Zips the sequence minus last element with itself minus first element, resulting in adjacent elements paired with each other | transformation |
| `enumerate(start=0)` | Zips the sequence with the index starting at `start` on the left side | transformation |
| `cartesian(*iterables, repeat=1)` | Returns cartesian product from itertools.product | transformation |
| `inner_join(other)` | Returns inner join of sequence with `other`. Must be a sequence of `(key, value)` pairs | transformation |
Expand Down
12 changes: 12 additions & 0 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,18 @@ def zip_with_index(self, start: int = 0) -> Sequence[tuple[_T_co, int]]:
"""
return self._transform(transformations.zip_with_index_t(start))

def zip_with_next(self) -> Sequence[tuple[_T_co, _T_co]]:
"""
Zips this sequence minus last element with itself minus first element,
resulting in sequence of each element paired with the next.

>>> seq([1, 2, 3, 4]).zip_with_next()
[(1, 2), (2, 3), (3, 4)]

:return: sequence of adjacent elements paired
"""
return self._transform(transformations.zip_with_next_t())

def enumerate(self, start: int = 0) -> Sequence[tuple[int, _T_co]]:
"""
Uses python enumerate to to zip the sequence with indexes starting at start.
Expand Down
15 changes: 15 additions & 0 deletions functional/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from functional.transformations import name
from functional import seq, pseq

from parametrize import parametrize # type: ignore

Data = namedtuple("Data", "x y")


Expand Down Expand Up @@ -831,6 +833,19 @@ def test_zip_with_index(self):
self.assertIteratorEqual(result, e)
self.assert_type(result)

@parametrize(
"sequence,expected",
[
([1, 2, 3, 4], [(1, 2), (2, 3), (3, 4)]),
([i for i in range(10_000)], [(i, i + 1) for i in range(10_000 - 1)]),
([], []),
],
)
def test_zip_with_next(self, sequence, expected):
result = self.seq(sequence).zip_with_next()
self.assertIteratorEqual(result, expected)
self.assert_type(result)

def test_to_list(self):
l = [1, 2, 3, "abc", {1: 2}, {1, 2, 3}]
result = self.seq(l).to_list()
Expand Down
4 changes: 4 additions & 0 deletions functional/test/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ def type_checking() -> None:
["a", "b", "c"]
).zip_with_index()

t_zip_with_next: Sequence[tuple[str, str]] = seq(
["a", "b", "c"]
).zip_with_next()

t_enumerate: Sequence[tuple[int, str]] = seq(["a", "b", "c"]).enumerate(start=1)

# t_inner_join: Sequence[tuple[str, Sequence[int]]] = seq([('a', 1), ('b', 2), ('c', 3)]).inner_join([('a', 2), ('c', 5)])
Expand Down
10 changes: 10 additions & 0 deletions functional/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ def zip_with_index_t(start):
)


def zip_with_next_t():
"""
Transformation for Sequence.zip_with_next
:return: transformation
"""
return Transformation(
"zip_with_next", lambda sequence: zip(sequence, sequence[1:]), None
)


def enumerate_t(start):
"""
Transformation for Sequence.enumerate
Expand Down
14 changes: 13 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ all = ["pandas"]

[tool.poetry.group.dev.dependencies]
black = "^23.1"
parametrize = "^0.1.1"
pytest = "^7.3.1"
pylint = "^3.3.3"
pytest-cov = "^4.0.0"
Expand Down