[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Recursive anonymous functions?
- From: Philip Bock <phil@...>
- Date: Tue, 20 Jul 2004 22:19:54 -0600
Is there any way for an anonymous function to call itself?
For example, if I'm writing a table serializer, like this one:
function serialize_table(table, outfile, tab_depth)
local indent = string.rep("\t", tab_depth)
for k, v in pairs(table) do
if (type(v) == "number" or type(v) == "string")
outfile:write(indent..k.." = "..v)
elseif (type(v) == "table")
serialize_table(table, outfile, tab_depth + 1)
function serialize(table, filename)
local outfile = io.open(filename, "w")
serialize_table(table, outfile, 1)
Is there any way to place the serialize_table function inside the
serialize function, so I don't have to pollute the global namespace, but
still let it call itself? I suppose I could declare it local inside the
serialize function, and then pass it as an argument to itself, but is
there a less ugly way to do it?
Thanks, Philip Bock