I have a generator function that goes something like this:
def mygenerator():
next_value = compute_first_value() # Costly operation
while next_value
For my use case I used a modified version of @ncoghlan answer but wrapped in a factory function to decorate the generating function:
import collections, functools, itertools
def primed_generator(generating_function):
@functools.wraps(generating_function)
def get_first_right_away_wrapper(*args,**kw):
"call the generator function, prime before returning"
gen = generating_function(*args,**kw)
assert isinstance(gen,collections.Iterator)
first_value = next(gen)
return itertools.chain((first_value,),gen)
return get_first_right_away_wrapper
Then just decorate the function:
@primed_generator
def mygenerator():
next_value = compute_first_value() # Costly operation
while next_value != terminating_value:
yield next_value
next_value = compute_next_value()
and the first value will be calculated immediately, and the result is transparent.