So this is the code for a few simple functions:
Code:
local ct = {
-- for ternery operator ? in C/Java
-- a little more stable than the standard use of
-- <cond> and <trueval> or <falseval>
tern = function (cond, trueVal, falseVal)
if cond then
return trueVal
else
return falseVal
end
end,
-- takes a list of statements and discards all
-- but the last one (which it returns)
series = function (a, b, ...)
if type(b) == "nil" then
return a
else
return ct.series (b, ...)
end
end,
}
I'm looking at the series function. If the comment is unclear, it basically just takes a series of values and returns on the last one. So the following code
Code:
inc = function()
i = i + 1
return i
end
dotest2 = function ()
print (ct.series(inc(),inc(),inc(),inc()) )
end
dotest2()
should print the value 4. However, I can't call ct.series in its own definition, presumably because the table doesn't exist at that point. I know that with functions you can use the sugar definition to avoid having to forward reference it, IE
Code:
function fact(i)
return i * fact(i-1)
end
is the same as
Code:
local fact
fact = function(i)
return i*fact(i-1)
end
but I can't get a good similar table forward referencing down. I have this working
Code:
local ct = {series}
ct = { <see above>}
but that spawns an unnecessary table. Is there a better way?