[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Unpack with multiple args
- From: "Aaron Brown" <aaron-lua@...>
- Date: Thu, 19 Aug 2004 17:23:27 -0400
Since we've been talking about some enhancements to Lua
(new ... syntax, select()), I thought I'd bring up
something. One time I wrote a curry function (see below)
and I found that most of the work was in writing a function
that works just like unpack but takes multiple arguments
(i.e., multunpack({1, 2}, {3, 4}) returns 1, 2, 3, and 4).
Is there a good reason why the stock unpack shouldn't do
this?
-- Act like unpack, but accept multiple arguments:
local function multunpack(...)
local ret = {}
for _, tbl in ipairs(arg) do
for _, rec in ipairs(tbl) do
table.insert(ret, rec)
end -- for _, rec
end -- for _, tbl
return unpack(ret)
end -- multunpack
-- Return a function such that calling it will be like
-- calling fnc with the arguments given to curry coming
-- first in the argument list; (curry(foo, 1, 2))(3, 4) is
-- equivalent to foo(1, 2, 3, 4).
function curry(fnc, ...)
local curriedargs = arg
return function(...)
return fnc(multunpack(curriedargs, arg))
end -- function
end -- curry
--
Aaron