-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathitertools.py
More file actions
48 lines (44 loc) · 932 Bytes
/
itertools.py
File metadata and controls
48 lines (44 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
##
# .python.itertools
##
"""
itertools extensions
"""
import collections.abc
from itertools import cycle, islice
def interlace(*iters, next = next) -> collections.abc.Iterable:
"""
interlace(i1, i2, ..., in) -> (
i1-0, i2-0, ..., in-0,
i1-1, i2-1, ..., in-1,
.
.
.
i1-n, i2-n, ..., in-n,
)
"""
return map(next, cycle([iter(x) for x in iters]))
def chunk(iterable, chunksize = 256):
"""
Given an iterable, return an iterable producing chunks of the objects
produced by the given iterable.
chunks([o1,o2,o3,o4], chunksize = 2) -> [
[o1,o2],
[o3,o4],
]
"""
iterable = iter(iterable)
last = ()
lastsize = chunksize
while lastsize == chunksize:
last = list(islice(iterable, chunksize))
lastsize = len(last)
yield last
def find(iterable, selector):
"""
Return the first item in the `iterable` that causes the `selector` to return
`True`.
"""
for x in iterable:
if selector(x):
return x