lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 14/12/2012 21:09, Rapin Patrick wrote:
In LuaDura, I wrote a function named `array_concat`.
An important difference compared to other implementations so far is that it
operates _recursively_.
Its goal is to "flatten" a hierarchy of arrays into a single array (or
sequence).

function dura.array_concat(...)
     local res, arg = {}, {...}
     local function insert(val)
         if type(val) == 'table' and (#val > 0 or next(val) == nil) then
             for _,j in ipairs(val) do
                 insert(j)
             end
         else
             table.insert(res, val)
         end
     end
     insert(arg)
     return res
end

Very useful (also in debug, for quick checks at complex data), always have it in my toolbox as well. But it's usually called 'flatten', precisely, 'concat' may be misleading here.

Denis