# For loop on a generator

I have written this function to iterate over tuples of integers of constant sum, but it doesn’t work when I jit it.
I understand the problem is in the line where there’s a `for` loop on the generator function itself.

``````from numba import njit

# @njit
def my_generator(N, k):
r"""Numba implementation of an iterator over tuples of N integers,
such that sum(tuple) == k.

Args:
N (int): number of elements in the tuple
k (int): sum of the elements
Returns:
tuple(int): a tuple of N integers
"""
if N == 1:
yield (k,)
else:
for i in range(k+1):
for j in my_generator(N-1, k-i):
yield (i,) + j
``````

is there a simple fix?

This works! (in reverse order, but for my application that’s ok)

``````import numpy as np
from numba import njit

@njit
def next_lst(lst, i, reset=False):
r"""Computes the next list of integers given the current list
and the current index.
"""
if lst[i] == 0:
return next_lst(lst, i+1, reset=True)
else:
lst[i] -= 1
lst[i+1] += 1
if reset:
lst = np.sum(lst[:i+1])
lst[1:i+1] = 0
i = 0
return lst, i

@njit
def generator(N, k):
r"""Goes through all the lists of integers of constant sum recursively.
"""
lst = np.zeros(N, dtype=np.int64)
lst = k
i = 0
yield lst
while lst[-1] < k:
lst, i = next_lst(lst, i)
yield lst
``````