2,875
edits
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
local self = setmetatable({}, funlist) | local self = setmetatable({}, funlist) | ||
self.mytable = tbl | |||
return self | |||
end | |||
-- Private functions. | |||
local function getElementAtIndex(index) | |||
assert(tonumber(index)) | |||
local ix = tonumber(index) | |||
local i = 0 | |||
if ix >= 0 then | |||
for _, v in pairs(mytable) do | for _, v in pairs(mytable) do | ||
if | i = i + 1 | ||
return | if i == ix then | ||
return true, v | |||
end | end | ||
end | end | ||
end | end | ||
-- Determines whether | return false, nil | ||
end | |||
-- Public functions | |||
-- Determines whether all elements of a sequence satisfy a condition. | |||
function funlist:all(predicate) | |||
assert(predicate) | |||
for _, v in pairs(self.mytable) do | |||
if predicate(v) == false then | |||
return false | |||
end | end | ||
end | end | ||
return true | |||
end | |||
-- Determines whether any element of a sequence exists or satisfies a condition. | |||
function funlist:any(predicate) | |||
if predicate then | |||
for _, v in pairs(self.mytable) do | |||
if predicate(v) == true then | |||
for _, v in pairs(mytable) do | |||
if | |||
return true | return true | ||
end | end | ||
end | end | ||
return | else | ||
for _, _ in pairs(self.mytable) do | |||
return true | |||
end | |||
end | end | ||
return false | |||
end | |||
-- Appends a value to the end of the sequence. | |||
function funlist:append(item) | |||
assert(item) | |||
table.insert(self.mytable, item) | |||
end | |||
-- Determines whether a sequence contains a specified element. | |||
function funlist:contains(item) | |||
assert(item) | |||
for _, v in pairs(self.mytable) do | |||
if funlist.isEqual(v, item) == true then | |||
return true | |||
end | end | ||
return count | end | ||
return false | |||
end | |||
-- Returns the number of elements in a sequence. | |||
function funlist:count() | |||
local count = 0 | |||
for _, _ in pairs(self.mytable) do | |||
count = count + 1 | |||
end | |||
return count | |||
end | |||
-- Returns the element at a specified index in a sequence. | |||
function funlist:itemAt(index) | |||
local found, item = getElementAtIndex(index) | |||
if found == true then | |||
return item | |||
end | end | ||
-- Returns the element at a specified index in a sequence. | error('Index out of range') | ||
end | |||
-- 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) | |||
local found, item = getElementAtIndex(index) | |||
if found == true then | |||
return item | |||
end | end | ||
return defaultItem | |||
end | |||
-- Returns the first element of a sequence. | |||
function funlist:first() | |||
for _, v in pairs(self.mytable) do | |||
return v | |||
end | end | ||
error('No items in sequence.') | |||
end | |||
-- Returns the first element of a sequence, or a default value if no element is found. | |||
function funlist:firstOrDefault(defaultItem) | |||
for _, v in pairs(self.mytable) do | |||
return v | |||
end | end | ||
return defaultItem | |||
end | |||
-- Returns the last element of a sequence. | |||
function funlist:last() | |||
if funlist.any() == false then | |||
error('Sequence contains no items.') | |||
end | end | ||
local last = nil | |||
for _, v in pairs(self.mytable) do | |||
last = v | |||
end | end | ||
return last | |||
end | |||
-- Returns the last element of a sequence, or a default value if no element is found. | |||
function funlist:lastOrDefault(defaultItem) | |||
if funlist.any() == false then | |||
return defaultItem | |||
end | end | ||
local last = nil | |||
for _, v in pairs(self.mytable) do | |||
last = v | |||
end | end | ||
return last | |||
end | |||
-- Returns the maximum value in a sequence of values. | |||
function funlist:max() | |||
local h = funlist:first() | |||
for _, v in pairs(self.mytable) do | |||
local num = tonumber(v) | |||
if num == nil then error('Value is NaN') end | |||
if v > h then h = v end | |||
end | end | ||
return h | |||
end | |||
-- Returns the minimum value in a sequence of values. | |||
function funlist:min() | |||
local l = funlist:first() | |||
for _, v in pairs(self.mytable) do | |||
local num = tonumber(v) | |||
if num == nil then error('Value is NaN') end | |||
if v < l then l = v end | |||
end | |||
return l | |||
end | end | ||
edits