Module:FunList: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 13: Line 13:


-- Private functions.
-- Private functions.
local function getElementAtIndex(index)
local function getElementAtIndex(tbl, index)
assert(tonumber(index))
assert(tonumber(index))
local ix = tonumber(index)
local ix = tonumber(index)
local i = 0
local i = 0
if ix >= 0 then
if ix >= 0 then
for _, v in pairs(self.mytable) do
for _, v in pairs(tbl) do
i = i + 1
i = i + 1
if i == ix then
if i == ix then
Line 85: Line 85:
-- Returns the element at a specified index in a sequence.
-- Returns the element at a specified index in a sequence.
function funlist:itemAt(index)
function funlist:itemAt(index)
local found, item = getElementAtIndex(index)
local found, item = getElementAtIndex(self.mytable, index)
if found == true then
if found == true then
return item
return item
Line 95: Line 95:
-- Returns the element at a specified index in a sequence or a default value if the index is out of range.
-- Returns the element at a specified index in a sequence or a default value if the index is out of range.
function funlist:itemAtOrDefault(index, defaultItem)
function funlist:itemAtOrDefault(index, defaultItem)
local found, item = getElementAtIndex(index)
local found, item = getElementAtIndex(self.mytable, index)
if found == true then
if found == true then
return item
return item
2,875

edits