Skip to content

Commit

Permalink
add YieldIf
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Jul 4, 2023
1 parent a32f664 commit b6e3909
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,21 @@ False

```

## YieldIf
Takes a function to map values (optional, by default there's no mapping) and a key. If key is false, value will not be yielded. Key is optional, default is `bool`

```py
>>> range(5) | YieldIf(lambda x: x * 100) | Pipe(list)
[100, 200, 300, 400]
>>> range(5) | YieldIf(lambda x: x * 100, key=lambda x: x % 2 == 0) | Pipe(list)
[0, 200, 400]
>>> range(5) | YieldIf(key=lambda x: x % 2 == 0) | Pipe(list)
[0, 2, 4]
>>> range(5) | YieldIf() | Pipe(list)
[1, 2, 3, 4]

```

## GetItem

```py
Expand Down
1 change: 1 addition & 0 deletions pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class StarMap (B): __ror__ = lambda self, x: x | Map(lambda y: y | PipeArgs
class MapApply (B): __ror__ = lambda self, it: it | Map(lambda x: x | Apply(self.f))
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 GetItem (B): __ror__ = lambda self, x: operator.getitem(x, self.f)
Expand Down
13 changes: 13 additions & 0 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,19 @@ def test_map_switch(it, cases, expected):
assert it | MapSwitch(cases) | Pipe(list) == expected


@pytest.mark.parametrize(
('it', 'f', 'key', 'expected'), [
(range(5), lambda x: x * 100, None, [100, 200, 300, 400]),
(range(5), lambda x: x * 100, lambda x: x % 2 == 0, [0, 200, 400]), (range(5), None, None, [1, 2, 3, 4]),
(range(5), None, lambda x: x % 2 == 0, [0, 2, 4]),
(range(5), None, None, [1, 2, 3, 4]),
],
)
def test_yield_if(it, f, key, expected):
y = YieldIf(f) if key is None else YieldIf(f, key=key)
assert it | y | 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 b6e3909

Please sign in to comment.