Thread Tools Display Modes
01-08-08, 01:29 PM   #1
Eidolarr
An Aku'mai Servant
 
Eidolarr's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 34
argh, recursion in tables

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?
  Reply With Quote
01-08-08, 02:11 PM   #2
Eidolarr
An Aku'mai Servant
 
Eidolarr's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 34
I posted this on the official forums now that I can finally log in. Please respond there if you can!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » argh, recursion in tables

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off