lua-users home
lua-l archive

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


What's the more "Lua" way to generically create a closure for a given
function and its arbitrary arg list than the following:

function closure(func,...)
    if arg.n == 0 then
        return function() return func() end
    elseif arg.n == 1 then
        return function() return func(arg[1]) end
    elseif arg.n == 2 then
        return function() return func(arg[1],arg[2]) end
    elseif arg.n == 3 then
        return function() return func(arg[1],arg[2],arg[3]) end
    elseif arg.n == 4 then
        return function() return func(arg[1],arg[2],arg[3],arg[4]) end
    elseif arg.n == 5 then
        return function() return
func(arg[1],arg[2],arg[3],arg[4],arg[5]) end
    else
        assert(false)
    end
end

Thanks,

Curt