micropython-lib/itertools/itertools.py

69 wiersze
1.4 KiB
Python
Czysty Zwykły widok Historia

def count(start=0, step=1):
2014-04-18 01:29:57 +00:00
while True:
yield start
start += step
def cycle(p):
try:
len(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)
p = cache
while p:
yield from p
2014-04-18 01:29:57 +00:00
def repeat(el, n=None):
if n is None:
while True:
yield el
else:
for i in range(n):
yield el
def chain(*p):
for i in p:
yield from i
def islice(p, start, stop=(), step=1):
if stop == ():
stop = start
start = 0
# TODO: optimizing or breaking semantics?
if start >= stop:
return
it = iter(p)
for i in range(start):
next(it)
2014-04-18 01:29:57 +00:00
while True:
yield next(it)
for i in range(step - 1):
next(it)
2014-04-18 01:29:57 +00:00
start += step
if start >= stop:
return
def tee(iterable, n=2):
return [iter(iterable)] * n
2015-12-08 09:16:36 +00:00
def starmap(function, iterable):
for args in iterable:
yield function(*args)
2017-04-24 13:56:44 +00:00
def accumulate(iterable, func=lambda x, y: x + y):
it = iter(iterable)
try:
acc = next(it)
except StopIteration:
return
yield acc
for element in it:
acc = func(acc, element)
yield acc