Skip to content

Commit

Permalink
refactor FlatMapValues and StarFlatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Dec 1, 2023
1 parent aa7576e commit 99e5b19
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ Same as `FilterKeys` but for `v` in `(k, v)` pairs
>>> [range(0, 5), range(100, 105)] | FlatMap(lambda it: (x for x in it if x % 2 == 0)) | Pipe(list)
[0, 2, 4, 100, 102, 104]

>>> [range(0, 5), range(100, 105)] | FlatMap(lambda it: it | Filter(lambda x: x % 2 == 0)) | Pipe(list)
[0, 2, 4, 100, 102, 104]

```

## FlatMapValues
Expand All @@ -152,8 +155,7 @@ Same as `FilterKeys` but for `v` in `(k, v)` pairs
>>> [("a", ["x", "y", "z"]), ("b", ["p", "r"])] | FlatMapValues(lambda x: x) | Pipe(list)
[('a', 'x'), ('a', 'y'), ('a', 'z'), ('b', 'p'), ('b', 'r')]

>>> keep_even = lambda it: it | Filter(lambda x: x % 2 == 0)
>>> [('a', [0, 1, 2]), ('b', [3, 4])] | FlatMapValues(keep_even) | Pipe(list)
>>> [('a', [0, 1, 2]), ('b', [3, 4])] | FlatMapValues(yield_even) | Pipe(list)
[('a', 0), ('a', 2), ('b', 4)]

```
Expand Down
4 changes: 2 additions & 2 deletions pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FilterFalse (B): __ror__ = lambda self, it: itertools.filterfalse(self.f,
class FilterKeys (B): __ror__ = lambda self, it: (kv for kv in it if (self.f or bool)(kv[0]))
class FilterValues (B): __ror__ = lambda self, it: (kv for kv in it if (self.f or bool)(kv[1]))
class FlatMap (B): __ror__ = lambda self, it: itertools.chain.from_iterable(self.f(x) for x in it)
class FlatMapValues(B): __ror__ = lambda self, it: it | FlatMap(lambda kv: ((kv[0], x) for x in self.f(kv[1])))
class FlatMapValues(B): __ror__ = lambda self, it: ((k, v) for k, vs in it for v in self.f(vs))
class KeyBy (B): __ror__ = lambda self, it: ((self.f(x), x) for x in it)
class ValueBy (B): __ror__ = lambda self, it: ((x, self.f(x)) for x in it)
class Append (B): __ror__ = lambda self, it: ((*x, self.f(x)) for x in it)
Expand All @@ -41,7 +41,7 @@ class ReduceByKey (B): __ror__ = lambda self, it: it | GroupBy(lambda kv: kv[0]
class Apply (B): __ror__ = lambda self, x: x | Exec(self.f, x)
class StarPipe (B): __ror__ = lambda self, x: self.f(*x)
class StarMap (B): __ror__ = lambda self, x: itertools.starmap(self.f, x)
class StarFlatMap (B): __ror__ = lambda self, x: itertools.chain.from_iterable(itertools.starmap(self.f, x))
class StarFlatMap (B): __ror__ = lambda self, x: itertools.starmap(self.f, x) | Pipe(itertools.chain.from_iterable)
class MapApply (B): __ror__ = lambda self, it: (x | Apply(self.f) for x in it)
class Switch (B): __ror__ = lambda self, x: next((v(x) for k, v in self.f if k(x)), x)
class MapSwitch (B): __ror__ = lambda self, it: (x | Switch(self.f) for x in it)
Expand Down

0 comments on commit 99e5b19

Please sign in to comment.