I found myself using these 2 custom generators and thinking “there’s got to be an itertools function or something that already does this! Didn’t find any though. Am I missing something? Thanks!
def gothru(iters):
for i in iters:
for j in i:
yield j
def overnover(fn,startval):
val = startval
while True:
val = fn(val)
yield val
EDIT: i was later imagining how overnover could be used to generate the fibonacci sequence, and i realized that it would need to be generalized to allow the function to have more than one argument
def overnover(fn,*args):
while True:
args = fn(*args)
return args
then you could do:
fibInfo = overnover(lambda x,y: (x+y, x), 1, 1)
-> (2,1) … (3, 2) … (5, 3) … (8, 5) …
and then:
fib = imap(lambda x:x[0], fibInfo)
-> 2 … 3 … 5 … 8 …
thanks guys!
The first one is
chain.from_iterable.The closest thing to
overnoveris something liketabulate:which is a special case of your function where it outputs sequential numbers.
countalso takes astep, so you could generalize this toHere is a version of
overnoverthat would let you send values into the sequence: