Skip to content

Commit f9b513e

Browse files
authored
add enumerate() pipe (#87)
1 parent 9243d9e commit f9b513e

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,20 @@ Deduplicate values, using the given `key` function if provided.
340340
```
341341

342342

343+
## `enumerate(start=0)`
344+
345+
The builtin `enumerate()` as a Pipe:
346+
347+
```python
348+
>>> from pipe import enumerate
349+
>>> list(['apple', 'banana', 'citron'] | enumerate)
350+
[(0, 'apple'), (1, 'banana'), (2, 'citron')]
351+
>>> list(['car', 'truck', 'motorcycle', 'bus', 'train'] | enumerate(start=6))
352+
[(6, 'car'), (7, 'truck'), (8, 'motorcycle'), (9, 'bus'), (10, 'train')]
353+
354+
```
355+
356+
343357
## `filter(predicate)`
344358

345359
Alias for `where(predicate)`, see `where(predicate)`.

pipe.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ def uniq(iterable, key=lambda x: x):
102102
prevkey = itemkey
103103

104104

105+
enumerate = Pipe(builtins.enumerate)
106+
107+
105108
@Pipe
106109
def permutations(iterable, r=None):
107110
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC

tests/test_pipe.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ def test_netcat():
3232
for packet in data | pipe.netcat("python.org", 80):
3333
response += packet.decode("UTF-8")
3434
assert "HTTP" in response
35+
36+
37+
def test_enumerate():
38+
data = [4, "abc", {"key": "value"}]
39+
expected = [(5, 4), (6, "abc"), (7, {"key": "value"})]
40+
assert list(data | pipe.enumerate(start=5)) == expected

0 commit comments

Comments
 (0)