2015-05-26 21:55:17 +00:00
|
|
|
def count(start=0, step=1):
|
2014-04-18 01:29:57 +00:00
|
|
|
while True:
|
|
|
|
yield start
|
|
|
|
start += step
|
|
|
|
|
|
|
|
def cycle(p):
|
2015-08-25 12:36:45 +00:00
|
|
|
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)
|
2015-08-25 18:27:54 +00:00
|
|
|
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
|
2015-08-25 18:17:34 +00:00
|
|
|
# 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:
|
2015-08-25 18:17:34 +00:00
|
|
|
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
|