reyield
- reyield(func: Callable[Concatenate[Iterable[X], P], None], elements: Iterable[X], *args: P.args, **kwargs: P.kwargs) Generator[X, None, None][source]
Make a function that consumes an iterable yield its elements.
- Parameters:
func – A function that consumes an iterable, and does not return anything
elements – An iterable
- Returns:
A generator that injects the elements of the iterable one at a time before yielding them.
>>> state = {"value": 0} >>> def accumulate(elements: Iterable[int]) -> None: ... for element in elements: ... state["value"] += element >>> square_sum = 0 >>> for element in reyield(accumulate, range(5)): ... square_sum += element**2
The goal of this function is not to need to keep elements in memory, so this should be an iterable-native alternative to just doing:
>>> elements = range(5) >>> elements = list(elements) >>> accumulate(elements) >>> square_sum = 0 >>> for element in elements: ... square_sum += element**2