tags:

views:

198

answers:

3

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

+2  A: 

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:

  1. Exactly how many times do you want to call each function? Will this be the same for each function? Will it vary?
  2. If the number of calls varies, what should determine this?
  3. 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.

Amber
A: 

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.

RCIX
gives me error "'do' expected near for"Also, what are you assuming the table is in this function? I'm guessing times is how many times i want the called?what is "..."?
Ninjai
See explanation, also tweaked my for loop to fix it.
RCIX
Modified it slightly to fix the missing `do`, as well as changed the outer loop from a `while` to a `for` since for basic numerical iteration, the `for` syntax is far more concise.
Amber
I am useing a table like: t={function(n) print(n+1) end, function(n) print(n+2)} When i use the function above I put: CallFuncs(3, t, 'foo')Gives me the error: Bad argument #1 to 'unpack' (table expected, got string)
Ninjai
Try it now. It should work, just tried it myself.
RCIX
Thank you! works now
Ninjai
A: 

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
mkluwe
fairly sure your code breaks with mutiple arguments passed into the `...` portion.
RCIX
Ok, so it doesn't.
RCIX
No, I think it's the standard way of handling variable length parameter lists.
mkluwe