Skip to content

Commit

Permalink
add Join
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Jul 4, 2023
1 parent 60abaaf commit 0c0d1aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,16 @@ Takes a function to map values (optional, by default there's no mapping) and a k

```

## Join
```py
>>> range(5) | Join(range(2, 5)) | Pipe(list)
[(2, 2), (3, 3), (4, 4)]

>>> range(1, 7) | Join(range(2, 6), key=lambda x, y: x % y == 0) | Pipe(list)
[(2, 2), (3, 3), (4, 2), (4, 4), (5, 5), (6, 2), (6, 3)]

```

## GetItem

```py
Expand Down
1 change: 1 addition & 0 deletions pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MapApply (B): __ror__ = lambda self, it: it | Map(lambda x: x | Apply(
class Switch (B): __ror__ = lambda self, x: self.f | FilterKeys(lambda p: p(x)) | Values() | Map(lambda f: f(x)) | Pipe(next, x)
class MapSwitch (B): __ror__ = lambda self, it: it | Map(lambda x: x | Switch(self.f))
class YieldIf (B): __ror__ = lambda self, it: ((self.f or (lambda y: y))(x) for x in it if self.kw.get('key', bool)(x))
class Join (B): __ror__ = lambda self, it: it | FlatMap(lambda x: ((x, y) for y in self.f if self.kw.get('key', operator.eq)(x, y)))


class GetItem (B): __ror__ = lambda self, x: operator.getitem(x, self.f)
Expand Down
11 changes: 11 additions & 0 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,17 @@ def test_yield_if(it, f, key, expected):
assert it | y | Pipe(list) == expected


@pytest.mark.parametrize(
('a', 'b', 'key', 'expected'), [
(range(5), range(2, 5), None, [(2, 2), (3, 3), (4, 4)]),
(range(1, 7), range(2, 6), lambda x, y: x % y == 0, [(2, 2), (3, 3), (4, 2), (4, 4), (5, 5), (6, 2), (6, 3)]),
],
)
def test_join(a, b, key, expected):
j = Join(b) if key is None else Join(b, key=key)
assert a | j | Pipe(list) == expected


@pytest.mark.parametrize(
('it', 'f', 'expected'), [
([('a', 1), ('b', 1), ('a', 1)], operator.add, [('a', 2), ('b', 1)]),
Expand Down

0 comments on commit 0c0d1aa

Please sign in to comment.