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 == (): if stop == ():
stop = start stop = start
start = 0 start = 0
# TODO: optimizing or breaking semantics?
if start >= stop:
return
it = iter(p)
for i in range(start):
next(it)
while True: while True:
try: yield next(it)
yield p[start] for i in range(step - 1):
except IndexError: next(it)
return
start += step start += step
if start >= stop: if start >= stop:
return return