Functions in a Table - Lua - lua

I have a table that has multiple functions in it. I'm trying to write a single function that will go through and use all the functions by passing random information into it.
Methods = {}
insert functions into Methods Table
function Methods:Multi() if #self > 0
then .........................
I'm guessing i need a loop that goes through the entire table but I can't do #self because i need it to do each function multiple times. Not sure how to pass in random info to the function either. Any help would be appreciated

Your problem doesn't seem to be all that specific - you're likely going to need to define exactly what you want to happen in more detail in order to be able to implement a program for it. (Sort of like if you told me "I need a program that calculates a number" - I'd probably respond "okay, what number do you want it to calculate?"
Things to consider:
Exactly how many times do you want to call each function? Will this be the same for each function? Will it vary?
If the number of calls varies, what should determine this?
How exactly do you want to determine what parameters are passed? Their types/count? Their values?
A very basic starting framework might look like this:
Methods = {}
-- Insert functions into Methods table
for _,func in ipairs(Methods) do
for i=1,5 do
func()
end
end
which would call each function 5 times, albeit w/o arguments.

I modified the above suggestion to
function CallFuncs(times, funcs, ...)
while (times > 0) do
for _, func in pairs(funcs) do
func(...)
end
times = times - 1
end
end
Example usage:
t = {
function( n ) print( n ) end,
function( n ) print( #n ) end,
}
CallFuncs( 2, t, 'foo' )
yields
foo
3
foo
3

Try this:
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
Usage:
local t = {
function(n) print(n) end,
function(n) print(n+1) end,
function(n) print(n+2) end
}
CallFuncs(3, t, 2)
This assumes all the functions in the table have more or less the same arguments.
Info:
The ... you see is lua's syntax for variable arguments (these are packed into a table and used as the last argument to the function), and the unpack function takes a table and multi-returns a series of values.

If you are asking for something to call a list of functions, I just typed this very small module for you (if you can even call it that).
-- No guarantees whether this will work.
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,...)
-- All parts of ... should be tables {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
-- These don't check for argument validity,
-- so you should either be careful with the arguments
-- or call these in protected mode (with pcall).
If I wasted my time with that, it was fun anyway, but after typing it I reread your question... You want something to iterate through a list of functions and pass a random number to each one.
function ExecuteWithRandomValues (func_list,async)
assert(type(func_list) == "table","Expected table.")
local passbacks = {}
local threads
if async then
threads = {}
end
for _,func in pairs(func_list) do
assert(type(func) == "function","Value [" .. _ .. "] is not a 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
-- Again, no guarantees this thing is free of errors.
-- There are better ways to run and check the statuses
-- of asynchronous threads, but this is fairly convenient.

Related

How to create a function with variable parameters just like table.insert()?

I know I can use function(a, b, ...) to solve variable parameters problems. But how can I do if I want to create a function like table.insert (table, [pos,] value) ?
Of cause, not in the following way:
function (table, pos, value)
if value == nil then
pos = value
value = nil
end
-- do something
end
Lua does not have function overloading or typechecking of arguments, if needed those should be implemented by the one needing.
Conveying the usage of your function is also up to you.
If you are not satisfied with your provided snippet, you can rewrite it in other manners for example :
function(arg1,arg2,arg3)
tab=arg1
if not arg3 then
value=arg2
index=#tab+1
else
assert(type(arg2)=='numer',"bad argument #2 to 'insert' (number expected, got table)")
value=arg3
index=arg2
end
table.insert(tab,index,value)
end
or:
f2=function(a,b)
--do smth
end
f3=function(a,b,c) end
f=function(...)
args={...}
nargin=#args
if nargin==2 then
f2(args[1],args[2]) --one way to use varied arguments
elseif nargin==3 then
f3(...) --other one
else error("wrong number of arguments")
end
end
I don't recommend you making functions with optional middle positional arguments though. At least not in a language without compile time type check, and even in those as well.
If you really want optional arguments, put them in the table:
function(args)
tab=args.table
index=args.index or #tab+1
value=args.value
--other arguments, options and associated logic here
table.insert(tab,index,value)
end

Caching expensive table calculation on strings between separate function calls in Lua

I have a number of functions operating on strings to extract interesting properties from those strings. One particular function which is called by many of those functions is very expensive and ultimately generates a table of values:
local function expensive(s)
local t = nil
return function()
if not t then
t = {}
-- some expensive operations with s which add items to t
end
return t
end
end
local function fn1(s)
local t = expensive(s)
-- some other fast operations using t and s
end
local function fn2(s)
local t = expensive(s)
-- some other fast operations using t and s
end
local s1, s2 = 'a', 'b'
fn1(s1) -- should create the 't' table for s1
fn1(s2) -- should create the 't' table for s2
fn2(s1) -- should not create the 't' table again for s1
fn1(s2) -- should also not create the 't' table again for s2
How can I make it so that the expensive function creates the table exactly once per string, returning the table in either case? I would rather not have the table exposed to the global environment. I think this could probably be accomplished by some clever use of closures, but I don't know the construct well enough.
local cache = {}
local function expensive(s)
local t = cache[s]
if not t then
t = {}
-- some expensive operations with s which add items to t
cache[s] = t
end
return t
end
Egor's answer will do the job, but the cache table is accessible by the whole file. To hide it, you have a couple of options. The first is a simple do/end block.
local expensive
do
local cache = {}
expensive = function (s)
local t = cache[s]
if not t then
t = {}
-- some expensive operations with s which add items to t
cache[s] = t
end
return t
end
end
The other is a self-executing function.
local expensive = (function ()
local cache = {}
return function (s)
local t = cache[s]
if not t then
t = {}
-- some expensive operations with s which add items to t
cache[s] = t
end
return t
end
end)()
The self-executing function has the advantage that you only have to define the expensive function name once, but the disadvantage that it is a little harder to read than the do/end block. Otherwise they are pretty much the same.
You can use "memoization" to cache the values returned by the function depending on the parameters. You can read a chapter on it in Programming in Lua and use one of the memoization modules that do the work for you, for example, memoize.
See also this SO answer for related interesting solutions.

Lua nested coroutines

I am trying to use the redis-lua library within copas. It requires some patching.
One problem is that redis-lua defines some iterators as coroutines, but these iterators perform network operations that can yield.
So, coroutine.yield is used for two very different things: for the iterator, and for copas. As network calls are nested within iterators, network yields are intercepted by the coroutine.wrap of the iterator, instead of being intercepted by copas.
The example below shows the problem :
local function iterator ()
for i = 1, 2 do
if i == 2 then coroutine.yield () end -- network yield
coroutine.yield () -- iterator yield
end
end
local citerator = coroutine.wrap (iterator)
local function loop () -- use of the iterator within a copas thread
while citerator () do end
end
local cloop = coroutine.create (loop)
while coroutine.resume (cloop) do end -- same as copas loop, executes the cloop thread
Is there a "standard" solution to this problem, still allowing to use coroutines for iterators?
I was able to make a small example work by "tagging" the yields (see below), but it is incompatible with existing code. I can leave the copas code unmodified, but have to update iterators in redis-lua.
local function wrap (f, my_tag)
-- same as coroutine.wrap, but uses my_tag to yield again
local co = coroutine.create (f)
return function ()
local t = table.pack (coroutine.resume (co))
local code = t [1]
local tag = t [2]
table.remove (t, 1)
table.remove (t, 1)
if tag == nil then
return
elseif my_tag == tag then
return table.unpack (t)
else
coroutine.yield (tag, table.unpack (t))
end
end
end
local Iterator = {} -- tag for iterator yields
local Network = {} -- tag for network yields
local function iterator ()
for i = 1, 2 do
if i == 2 then coroutine.yield (Network, i) end
coroutine.yield (Iterator, i)
end
end
local citerator = wrap (iterator, Iterator)
local function loop ()
while citerator () do end
end
local cloop = wrap (loop, Network)
while cloop () do end
Is there a better solution?
Lua coroutine always yield to the last thread they were resumed from. The Copas socket functions expect to yield back to the Copas event loop, but instead they get stuck with the coroutine used to implement the redis-lua iterators. Unfortunately there isn't much you can do to fix that except changing the code of the redis-lua iterators. The reason why no one has done so yet is that until Lua 5.2 (LuaJIT can do it as well) it wasn't even possible to yield from an iterator function (the iterator yields in redis-lua work fine because they never leave the iterator function, but you cannot yield beyond the for loop like the Copas socket functions would try to do).
Your idea about using a tag value for distinguishing the iterator yields from the rest is good. You just have to make sure that you pass all yields not intended for the iterator function to the coroutine one level up, including any arguments/return values of coroutine.yield and coroutine.resume (the later is implicit when calling the coroutine.wraped function).
More specifically, if you have code like this in redis-lua:
-- ...
return coroutine.wrap( function()
-- ...
while true do
-- ...
coroutine.yield( some_values )
end
end )
You change it to:
-- ...
local co_func = coroutine.wrap( function()
-- ...
while true do
-- ...
coroutine.yield( ITERATOR_TAG, some_values ) -- mark all iterator yields
end
return ITERATOR_TAG -- returns are also intended for the iterator
end )
return function()
return pass_yields( co_func, co_func() ) -- initial resume of the iterator
end
The ITERATOR_TAG and the pass_yields function go somewhere near the top of redis.lua:
local ITERATOR_TAG = {} -- unique value to mark yields/returns
local function pass_yields( co_func, ... )
if ... == ITERATOR_TAG then -- yield (or return) intended for iterator?
return select( 2, ... ) -- strip the ITERATOR_TAG from results and return
else
-- pass other yields/resumes back and forth until we hit another iterator
-- yield (or return); using tail recursion here instead of a loop makes
-- handling vararg lists easier.
return pass_yields( co_func, co_func( coroutine.yield( ... ) ) )
end
end
AFAIK, the redis-lua developers are planning to tag another release by the end of the year, so they probably will be grateful for pull requests.
In your first example, you are using wrap(loop). I suppose this is copas' wrap, because there is no reference to copas in this code...
However, you are supposed to copas.wrap() a socket, but your loop is a function!
See copas documentation for a good introduction.

In Lua, is there a function that given a function, it returns its name as a string?

Sorry if this is too obvious, but I am a total newcomer to lua, and I can't find it in the reference.
Is there a NAME_OF_FUNCTION function in Lua, that given a function gives me its name so that I can index a table with it? Reason I want this is that I want to do something like this:
local M = {}
local function export(...)
for x in ...
M[NAME_OF_FUNCTION(x)] = x
end
end
local function fun1(...)
...
end
local function fun2(...)
...
end
.
.
.
export(fun1, fun2, ...)
return M
There simply is no such function. I guess there is no such function, as functions are first class citizens. So a function is just a value like any other, referenced to by variable. Hence the NAME_OF_FUNCTION function wouldn't be very useful, as the same function can have many variable pointing to it, or none.
You could emulate one for global functions, or functions in a table by looping through the table (arbitrary or _G), checking if the value equals x. If so you have found the function name.
a=function() print"fun a" end
b=function() print"fun b" end
t={
a=a,
c=b
}
function NameOfFunctionIn(fun,t) --returns the name of a function pointed to by fun in table t
for k,v in pairs(t) do
if v==fun then return k end
end
end
print(NameOfFunctionIn(a,t)) -- prints a, in t
print(NameOfFunctionIn(b,t)) -- prints c
print(NameOfFunctionIn(b,_G)) -- prints b, because b in the global table is b. Kind of a NOOP here really.
Another approach would be to wrap functions in a table, and have a metatable set up that calls the function, like this:
fun1={
fun=function(self,...)
print("Hello from "..self.name)
print("Arguments received:")
for k,v in pairs{...} do print(k,v) end
end,
name="fun1"
}
fun_mt={
__call=function(t,...)
t.fun(t,...)
end,
__tostring=function(t)
return t.name
end
}
setmetatable(fun1,fun_mt)
fun1('foo')
print(fun1) -- or print(tostring(fun1))
This will be a bit slower than using bare functions because of the metatable lookup. And it will not prevent anyone from changing the name of the function in the state, changing the name of the function in the table containing it, changing the function, etc etc, so it's not tamper proof. You could also strip the tables of just by indexing like fun1.fun which might be good if you export it as a module, but you loose the naming and other tricks you could put into the metatable.
Technically this is possible, here's an implementation of the export() function:
function export(...)
local env = getfenv(2);
local funcs = {...};
for i=1, select("#", ...) do
local func = funcs[i];
for local_index = 1, math.huge do
local local_name, local_value = debug.getlocal(2, local_index);
if not local_name then
break;
end
if local_value == func then
env[local_name] = local_value;
break;
end
end
end
return env;
end
It uses the debug API, would require some changes for Lua 5.2, and finally I don't necessarily endorse it as a good way to write modules, I'm just answering the question quite literally.
Try this:
http://pgl.yoyo.org/luai/i/tostring
tostring( x ) should hopefully be what you are looking for
If I am not wrong (and I probably will, because I actually never programmed in Lua, just read a bunch of papers and articles), internally there is already a table with function names (like locals and globals in Python), so you should be able to perform a reverse-lookup to see what key matches a function reference.
Anyway, just speculating.
But the fact is that looking at your code, you already know the name of the functions, so you are free to construct the table. If you want to be less error prone, it would be easier to use the name of the function to get the function reference (with eval or something like that) than the other way around.

What's the difference between these two Lua examples? Is one better?

I'm just getting started with Lua. In the example I'm learning from (the Ghosts & Monsters Corona open source), I see this pattern repeatedly.
local director = require("director")
local mainGroup = display.newGroup()
local function main()
mainGroup:insert(director.directorView)
openfeint = require ("openfeint")
openfeint.init( "App Key Here", "App Secret Here", "Ghosts vs. Monsters", "App ID Here" )
director:changeScene( "loadmainmenu" )
return true
end
main()
Is this some sort of convention experienced Lua programmers recommend or are there genuine advantages to doing it this way? Why wouldn't you just skip the function all together and do this:
local director = require("director")
local mainGroup = display.newGroup()
mainGroup:insert(director.directorView)
local openfeint = require ("openfeint")
openfeint.init( "App Key Here", "App Secret Here", "Ghosts vs. Monsters", "App ID Here" )
director:changeScene( "loadmainmenu" )
Is there some implicit benefit to the first style over the second? Thanks!
Is this some sort of convention experienced Lua programmers recommend or are there genuine advantages to doing it this way?
It's not typical. The advantage is that object state is private, but that's not enough of an advantage to recommend it.
I see this pattern repeatedly.
I've never seen it before, and it happens only once in the source you posted.
EDIT: Adding a response to a question asked in the comments below this post.
A function which accesses external local variables binds to those variables and is called a 'closure'. Lua (for historical reasons) refers to those bound variables as 'upvalues'. For example:
local function counter()
local i = 1
return function()
print(i)
i = i + 1
end
end
local a, b = counter(), counter()
a() a() a() b() --> 1 2 3 1
a and b are closures bound to different copies of i, as you can see from the output. In other words, you can think of a closure as function with it's own private state. You can use this to simulate objects:
function Point(x,y)
local p = {}
function p.getX() -- syntax sugar for p.getX = function()
return x
end
function p.setX(x_)
x = x_
end
-- for brevity, not implementing a setter/getter for y
return p
end
p1 = Point(10,20)
p1.setX(50)
print(p1.getX())
Point returns a table of closures, each bound to the locals x and y. The table doesn't contain the point's state, the closures themselves do, via their upvalues. An important point is that each time Point is called it creates new closures, which is not very efficient if you have large quantities of objects.
Another way of creating classes in Lua is to create functions that take a table as the first argument, with state being stored in the table:
function Point(x,y)
local p = {x=x,y=y}
function p:getX() -- syntax sugar for p.getX = function(self)
return self.x
end
function p:setX(x)
self.x = x
end
return p
end
p1 = Point(10,20)
p1:setX(50) -- syntax sugar for p1.setX(p1, 50)
print(p1:getX()) -- syntax sugar for p1.getX(p1)
So far, we're still creating new copies of each method, but now that we're not relying on upvalues for state, we can fix that:
PointClass = {}
function PointClass:getX() return self.x end
function PointClass:setX(x) self.x = x end
function Point(x,y)
return {
x = x,
y = y,
getX = PointClass.getX,
setX = PointClass.getY,
}
end
Now the methods are created once, and all Point instances share the same closures. An even better way of doing this is to use Lua's metaprogramming facility to make new Point instances automatically look in PointClass for methods not found in the instance itself:
PointClass = {}
PointClass.__index = PointClass -- metamethod
function PointClass:getX() return self.x end
function PointClass:setX(x) self.x = x end
function Point(x,y)
return setmetatable({x=x,y=y}, PointClass)
end
p1 = Point(10,20)
-- the p1 table does not itself contain a setX member, but p1 has a metatable, so
-- when an indexing operation fails, Lua will look in the metatable for an __index
-- metamethod. If that metamethod is a table, Lua will look for getX in that table,
-- resolving p1.setX to PointClass.setX.
p1:setX(50)
This is a more idiomatic way of creating classes in Lua. It's more memory efficient and more flexible (in particular, it makes it easy to implement inheritance).
I frequently write my own Lua scripts this way because it improves readability in this case:
function main()
helper1( helper2( arg[1] ) )
helper3()
end
function helper1( foo )
print( foo )
end
function helper2( bar )
return bar*bar
end
function helper3()
print( 'hello world!' )
end
main()
This way the "main" code is at the top, but I can still define the necessary global functions before it executes.
A simple trick, really. I can't think of any reason to do this besides readability.
The first style could be used too improove readability, but I would rather give the function some meaningful name instead of main or just go without the function.
By the way, I think it's always a good practice to name blocks of code, i.e. put them into functions or methods. It helps explain your intend with that piece of code, and encourages reuse.
I don't see much point to the first style as you've shown it. But if it said something like if arg then main() end at the bottom, the script might (just might) be useful as a loadable "library" in addition to being a standalone script. That said, having a main() like that smacks of C, not Lua; I think you're right to question it.

Resources