I often have a complex loop structure that must be applied to various parts of my script, just changing the core of the loop. Ex:
for a=1, 3 do
for b=a, 3 do
print(a, b)
end
end
print('---------')
for a=1, 3 do
for b=a, 3 do
print(a+1) --> same loop, other core
print(b+1)
end
end
Currently, to achieve this goal, I use the following structure:
local MyFunctions = {}
function MyFunctions.ScriptA(a, b, Arg1, Arg2)
print(a+Arg1, b+Arg2)
end
function MyFunctions.ScriptB(a, b, Text)
print(Text, a, b)
end
function MyLoop(FuncName, ...) -- the main loop
for a=1, 3 do
for b=a, 3 do
MyFunctions[FuncName](a, b, ...)
end
end
end
MyLoop('ScriptA', 10, 20)
print('-----')
MyLoop('ScriptB', 'some text')
This will work, and will print the following:
11 21
11 22
11 23
12 22
12 23
13 23
-----
some text 1 1
some text 1 2
some text 1 3
some text 2 2
some text 2 3
some text 3 3
But I think this structure is very bureaucratic…
So I would like to know if there is a simpler, more objective and elegant way to do this…