表中的函数- Lua
2009-11-1 10:14:32
收藏:0
阅读:270
评论:4
我有一个包含多个函数的表格。我正在尝试编写一个单一函数,通过向其中传递随机信息,可以遍历并使用所有函数。
Methods = {}
将函数插入到Methods表格中
function Methods:Multi() if #self > 0 then .........................
我猜我需要循环遍历整个表格,但我不能使用#self,因为我需要将每个函数执行多次。也不确定如何将随机信息传递给函数。任何帮助将不胜感激。
原文链接 https://stackoverflow.com/questions/1656803
点赞
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
我对上面的建议进行了修改:
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
如果你想要一个称作函数列表的东西,我为你写了一个非常小的模块(就算你都不想叫它模块的话)。
-- 不保证这个代码能正常运行。
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
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
您的问题似乎不是很具体 - 您可能需要更详细地定义您想要发生的事情,以便能够为其实现一个程序。 (就像如果您告诉我“我需要一个计算数字的程序” - 我可能会回答“好的,您要计算什么数字?”
需要考虑的事项:
一个非常基本的起始框架可能像这样:
`` 方法 = {}
-- 将函数插入到方法表中
for _, func in ipairs(Methods)do for i = 1,5 do func() end end ``
这将调用每个函数5次,尽管没有参数。