numba recursion
Note that an explicit type signature is required at the moment for Numba's recursion support to work
NG
code:python
import sys
import numba
def recur(t):
if t == 0:
return 1
return t * recur(t - 1)
print("compiling")
from numba.pycc import CC
cc = CC('my_module')
cc.export('recur', 'i8(i8)')(recur)
cc.compile()
else:
from my_module import recur
print(recur(5))
code::
Untyped global name 'recur': cannot determine Numba type of <class 'function'>
File "numbatest.py", line 7:
def recur(t):
<source elided>
return 1
return t * recur(t - 1)
^
OK
code:python
@numba.njit("i8(i8)")
def recur(t):
NG
code:python
def main(x):
def recur(t):
Not Supported
Numba now supports inner functions as long as they are non-recursive and only called locally, but not passed as argument or returned as result. The use of closure variables (variables defined in outer scopes) within an inner function is also supported.