kopia lustrzana https://github.com/micropython/micropython-lib
itertools: Make cycle() work with finite generators
rodzic
ebb0c60fac
commit
2e3e764f80
|
@ -4,8 +4,19 @@ def count(start=0, step=1):
|
||||||
start += step
|
start += step
|
||||||
|
|
||||||
def cycle(p):
|
def cycle(p):
|
||||||
while p:
|
try:
|
||||||
yield from p
|
len(p)
|
||||||
|
while p:
|
||||||
|
yield from p
|
||||||
|
except TypeError:
|
||||||
|
# len() is not defined for this type. Assume it is
|
||||||
|
# a finite iterable so we must cache the elements.
|
||||||
|
cache = []
|
||||||
|
for i in p:
|
||||||
|
yield i
|
||||||
|
cache.append(i)
|
||||||
|
while cache:
|
||||||
|
yield from cache
|
||||||
|
|
||||||
def repeat(el, n=None):
|
def repeat(el, n=None):
|
||||||
if n is None:
|
if n is None:
|
||||||
|
|
Ładowanie…
Reference in New Issue