This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChiknNuggets's profile picture

creating a function from a table

Started by ChiknNuggets, 06 October 2012 - 08:34 AM
ChiknNuggets #1
Posted 06 October 2012 - 10:34 AM
well i kinda know what to do here ive got it set out like this

func = {print("hello");print("bob")}

and i know i can call a function in it like

func = {function() print("hello");print("bob")} end}


what i want to know is how could i make it so that it automatically turns whats in the func (so the whole range) into a useable code
jag #2
Posted 06 October 2012 - 10:38 AM
I don't think it's possible, without like making a temporary file and put in the table there, then load the file with os.loadAPI()
ChiknNuggets #3
Posted 06 October 2012 - 10:40 AM
well i dont see how if i can use

func = {function() print("hello");print("bob")} end}

i dont see why i wouldnt be able to do something like concatenate them together
Luanub #4
Posted 06 October 2012 - 11:32 AM
Try using this type of format

local func = {}

func[1] = function()
print("hello")
print("bob")
end

func[2] = function()
--put something else here
end

for x=1, #func do
func[x]()
end
ChiknNuggets #5
Posted 06 October 2012 - 11:35 AM
I thought of that but the problem is then because im using it as an API it would be very user unfriendly, i know i can do it in a single line thing but having to add the function() and the end annoys me
Luanub #6
Posted 06 October 2012 - 11:37 AM
Instead of adding the function() portion to the table try using loadstring() instead. I've never used it so I'm not sure how it works but from what I know it might work for what you're wanting to achieve.
ChiknNuggets #7
Posted 06 October 2012 - 11:39 AM
it wouldnt work as the name suggests its only works for strings and mine arent actually stored as strings, i just thought there might be a way to call the function somehow
ChiknNuggets #8
Posted 06 October 2012 - 11:41 AM
something like {function() func[1]() end} maybe ?
Luanub #9
Posted 06 October 2012 - 11:42 AM
you can always turn them into a string here's how i think it would work. I'll go try it.

func ={"print("hello")}

loadstring(tostring(func[1]))

EDIT: Here try this

func ={"print('Hello ') print('Bob')"}
local run = loadstring(tostring(func[1]))
run()

Once you add more just use a for loop to execute

for x=1, #func do
local run = loadstring(tostring(func[x]))
run()
end
Edited on 06 October 2012 - 09:50 AM
ChiknNuggets #10
Posted 06 October 2012 - 11:58 AM
Nope tryed and didnt work

ill try the edit now
ChiknNuggets #11
Posted 06 October 2012 - 12:11 PM
ahh see i dont wanna have to edit the functions themselves, i just want to run them without change
Jarle212 #12
Posted 06 October 2012 - 04:44 PM
Use:

func ={function() print("Hello")end, function() print("Bob") end}
for i=1,#func do
func[i]();
end
MysticT #13
Posted 06 October 2012 - 06:12 PM
Ok, first of all, what are you trying to do? You're probably going the wrong way, but to help you we need to know what you want to do.
Kingdaro #14
Posted 06 October 2012 - 07:15 PM
I believe he's trying to turn

function foo()
print 'foo'
end

function bar()
print 'bar'
end

into


function foobar()
print 'foo'
print 'bar'
end

I can see this being useful in a lot of situations, and I think I might know of a way to do so.


function group(functs)
 local tfunct = {parts = {}}
 for i,v in pairs(functs) do
  table.insert(tfunct.parts, v)
 end
 
 local mt = {__call = function()
  for i,v in pairs(tfunct.parts) do
   v()
  end
 end}
 
 return setmetatable(tfunct, mt)
end

local function foo() print 'foo' end
local function bar() print 'bar' end

local foobar = group{foo, bar}

foobar()

--> foo
--> bar

This bit of code defines a grouping function that throws all of the functions in a table given into another table, and with some metatables magic, is callable, where it will call all of it's parts in the order given.