Lua:任意数量的返回值
在长时间只使用 C++ 之后,我又回到了 Lua,目前我正在尝试再次思考一些更复杂的事情。
想象一个小的实用函数,看起来像这样,为任意数量的参数多次调用一个函数:
-- helper to call a function multiple times at once
function smartCall(func, ...)
-- the variadic arguments
local args = {...}
-- the table to save the return values
local ret = {}
-- iterate over the arguments
for i,v in ipairs(args) do
-- if it is a table, we unpack the table
if type(v) == "table" then
ret[i] = func(unpack(v))
else
-- else we call the function directly
ret[i] = func(v)
end
end
-- return the individual return values
return unpack(ret)
end
然后我可以做这样的事情:
local a,b,c = smartCall(math.abs, -1, 2.0, -3.0)
local d,e,f = smartCall(math.min, {1.0, 0.3}, {-1.0, 2.3}, {0.5, 0.7})
这可行,但我想知道是否有更方便的方法,因为我的版本涉及大量的解包和临时表。
蒂
I am coming back to Lua after a long time of C++ only and I am currently trying to wrap my head around some of the more complicated things again.
imagine a small utility function, that looks like this, to call a function multiple times for an arbitrary number of arguments:
-- helper to call a function multiple times at once
function smartCall(func, ...)
-- the variadic arguments
local args = {...}
-- the table to save the return values
local ret = {}
-- iterate over the arguments
for i,v in ipairs(args) do
-- if it is a table, we unpack the table
if type(v) == "table" then
ret[i] = func(unpack(v))
else
-- else we call the function directly
ret[i] = func(v)
end
end
-- return the individual return values
return unpack(ret)
end
then I can do something like this:
local a,b,c = smartCall(math.abs, -1, 2.0, -3.0)
local d,e,f = smartCall(math.min, {1.0, 0.3}, {-1.0, 2.3}, {0.5, 0.7})
this works, but I am wondering if there is a more convinient way, since my version involves alot of unpack and temporary tables.
ty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您用 C 语言编写
smartCall
,它会更简单,并且不需要创建表。不过我不知道这对你是否方便。If you write
smartCall
in C, it'll be simpler and you won't need to create tables. I don't know whether this is convenient for you, though.有一段时间,我考虑将所有内容作为字符串传递,然后操作该字符串以进行有效的函数调用,并使用 tostring 来调用它;就在这时我意识到这根本不比在这里拆包更有效率。
然后我考虑添加一个额外的参数来指定您想要智能调用的函数的参数数量。这样,对于具有固定数量参数的函数,
smartCall
可以将其参数组传递给被调用的函数。同样,这需要提取表部分或算术来查找参数编号。所以,我想不出任何更简单的功能。而且
unpack
足够高效,不会对此类调用的整体执行时间产生重大影响。For a moment I thought about passing everything as strings then manipulating the string to make a valid function call and calling it using
tostring
; just then I realized it's not at all more efficient than unpacking here.Then I thought about putting an extra argument which specifies the number of arguments of the function you want to smartly call. This way for functions with a fixed number of arguments
smartCall
can pass groups of its arguments to the function being called. Again, this one needs extracting table parts or arithmetic to find argument numbers.So, I can't think of any simpler function. And
unpack
is efficient enough and it doesn't majorly affect the overall execution time of such calls.