表中的函数- Lua

我有一个包含多个函数的表格。我正在尝试编写一个单一函数,通过向其中传递随机信息,可以遍历并使用所有函数。

Methods = {}

将函数插入到Methods表格中

function Methods:Multi() if #self > 0 then .........................

我猜我需要循环遍历整个表格,但我不能使用#self,因为我需要将每个函数执行多次。也不确定如何将随机信息传递给函数。任何帮助将不胜感激。

原文链接 https://stackoverflow.com/questions/1656803

点赞
stackoverflow用户148870
stackoverflow用户148870

您的问题似乎不是很具体 - 您可能需要更详细地定义您想要发生的事情,以便能够为其实现一个程序。 (就像如果您告诉我“我需要一个计算数字的程序” - 我可能会回答“好的,您要计算什么数字?”

需要考虑的事项:

  1. 您想要调用每个函数的次数是多少?这对于每个函数都是相同的吗?会变化吗?
  2. 如果调用次数不同,是什么决定了这一点?
  3. 您想要如何确定传递的参数?它们的类型/数量?它们的值?

一个非常基本的起始框架可能像这样:

`` 方法 = {}

-- 将函数插入到方法表中

for _, func in ipairs(Methods)do for i = 1,5 do func() end end ``

这将调用每个函数5次,尽管没有参数。

2009-11-01 10:20:39
stackoverflow用户117069
stackoverflow用户117069

尝试这个:

function CallFuncs(times, funcs, ...)
    for i=1,times do
        for _, func in pairs(funcs) do
            if type(...) == "table" then func(unpack(...)) else func(...) end
        end
    end
end

用法:

local t = {
    function(n) print(n) end,
    function(n) print(n+1) end,
    function(n) print(n+2) end
}

CallFuncs(3, t, 2)

这假定表中的所有函数有更多或更少相同的参数。

信息: 你看到的...是lua的变量参数语法(这些封装到一个表中并用作函数的最后一个参数),而unpack函数接受一个表并多次返回一系列值。

2009-11-01 10:40:24
stackoverflow用户199201
stackoverflow用户199201

我对上面的建议进行了修改:

function CallFuncs(times, funcs, ...)
    while (times > 0) do
        for _, func in pairs(funcs) do
            func(...)
        end
        times = times - 1
    end
end

使用示例:

t = {
    function( n ) print( n ) end,
    function( n ) print( #n ) end,
}

CallFuncs( 2, t, 'foo' )

输出结果:

foo
3
foo
3
2009-11-01 11:35:42
stackoverflow用户1917534
stackoverflow用户1917534

如果你想要一个称作函数列表的东西,我为你写了一个非常小的模块(就算你都不想叫它模块的话)。

-- 不保证这个代码能正常运行。
function ExecFunc(fnctn, nTimes, Threaded, ...)
    local to_call = function()
        fnctn(...)
    end
    for x = 1, nTimes do
        if Threaded then
            coroutine.resume(coroutine.create(to_call))
        else
            to_call()
        end
    end
end

function ExecFuncs(Async, ...)
    -- 参数...的所有部分都应该是以下形式的表格 { function f, number t[, table args] }
    local funcInfo = {...}
    for _, funcThing in pairs(funcInfo) do
        local a = funcThing.args
        if a then
            ExecFunc(funcThing.f, funcThing.t, Async, unpack(a))
        else
            ExecFunc(funcThing.f, funcThing.t, Async)
        end
    end
end

-- 这些函数不会检查输入参数的有效性,所以要么小心使用参数,要么在保护模式下调用它们(使用 pcall 函数)。

如果我浪费时间写了这个东西,那还是挺有趣的。但是当我写完之后又重新看了一下你的问题……你想要的是一个可以迭代函数列表,并且对每个函数都传递一个随机数的东西。

function ExecuteWithRandomValues(func_list, async)
    assert(type(func_list) == "table", "参数必须是表格类型")
    local passbacks = {}
    local threads
    if async then
        threads = {}
    end
    for _, func in pairs(func_list) do
        assert(type(func) == "function", "第 [" .. _ .. "] 个值不是函数")
        local toCall = function()
            local rnd = math.random(-math.huge, math.huge)
            local out = func(rnd)
            passbacks[_] = {input = rnd, output = out}
        end
        if async then
            table.insert(threads, coroutine.create(toCall))
        else
            toCall()
        end
    end
    if async then
        for _, thread in pairs(threads) do
            coroutine.resume(thread)
        end
        for _, thread in pairs(threads) do
            while coroutine.status(thread) ~= "dead" do
            end
        end
    end
    return passbacks
end
-- 再次提醒,这个函数不保证没有错误。
-- 检查异步线程的状态有更好的方法,但这个方法比较方便。
2012-12-20 04:30:38