lua-users home
lua-l archive

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


Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> function cl(f,...) return function() pcall(f,unpack(arg)) end end
> f = cl(print,1,2,3)
> f()
1       2       3



> -----Original Message-----
> From: Curt Carpenter [mailto:curtc@microsoft.com]
> Sent: Wednesday, July 02, 2003 4:17 PM
> To: Lua list
> Subject: There's gotta be a more "Lua" way to do this
> 
> 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