Lua:任意数量的返回值

发布于 2024-12-24 03:04:39 字数 914 浏览 0 评论 0原文

在长时间只使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屌丝范 2024-12-31 03:04:39

如果您用 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.

泅渡 2024-12-31 03:04:39

有一段时间,我考虑将所有内容作为字符串传递,然后操作该字符串以进行有效的函数调用,并使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文