itertools: Make islice() work with arbitrary iterables, not just sequences.

pull/43/head
Paul Sokolovsky 2015-08-25 21:17:34 +03:00
rodzic 9a81d0ec25
commit 7bbdc050d3
1 zmienionych plików z 10 dodań i 4 usunięć

Wyświetl plik

@ -34,11 +34,17 @@ def islice(p, start, stop=(), step=1):
if stop == ():
stop = start
start = 0
while True:
try:
yield p[start]
except IndexError:
# TODO: optimizing or breaking semantics?
if start >= stop:
return
it = iter(p)
for i in range(start):
next(it)
while True:
yield next(it)
for i in range(step - 1):
next(it)
start += step
if start >= stop:
return