Actually I think you have come up with a nice and elegant solution. What you are doing is exploiting that functions in Lua are closures, this can be a very useful technique when writing recursive function, which needs to build data structures while running. All you need to do to make it perfect, is to add the local keyword in front of function findall
inside function findall_wrapper
, then your helper function will be local, and wont pollute the global namespace.
To elaborate a bit:
There are two different type of functions, simple recursive functions and complex recursive functions. All recursive functions can be implemented in the following way:
function sum_list(l)
if #l == 0 then
return 0
else
local e = table.remove(l)
return e + sum_list(l)
end
end
print(sum_list({1,2,3,4}))
> 10
Here the call stack is used to store intermediate results, this can give you a very large stack with deep recursion or multiple calls to the function in the return.
A better way of doing it is called tail-recursion:
function sum_list(l, a)
if #l == 0 then
return a
else
local e = table.remove(l)
return sum_list(l, a + e)
end
end
print(sum_list({1,2,3,4}), 0)
> 10
In this example an accumulator is passed in the call, so the call stack is no longer used for storage, and if the implementation supports it, it can turn it into an iteration. Sadly not all recursive functions are tail-recursive. The problem with the accumulator in this case is that people have to instantiate it to zero, otherwise it gives a wrong result.
The solution to this, is just what you did:
function sum_list(l)
local function sum_list_helper(l, a)
if #l == 0 then
return a
else
local e = table.remove(l)
return sum_list_helper(l, a + e)
end
end
return sum_list_helper(l, 0)
end
where a local function is created, which is then called with the correct instantiation value.