Module:FunList: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 8: Line 8:
      
      
     local self = setmetatable({}, funlist)
     local self = setmetatable({}, funlist)
self.mytable = tbl
-- Private fields
return self
local mytable = tbl
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
i = i + 1
if i == ix then
return true, v
end
end
end
return false, nil
end


-- Public functions
-- Private functions.
-- Determines whether all elements of a sequence satisfy a condition.
local function getElementAtIndex(index)
function funlist:all(predicate)
assert(tonumber(index))
assert(predicate)
local ix = tonumber(index)
local i = 0
if ix >= 0 then
for _, v in pairs(mytable) do
for _, v in pairs(mytable) do
if predicate(v) == false then
i = i + 1
return false
if i == ix then
return true, v
end
end
end
end
return true
end
end
-- Determines whether any element of a sequence exists or satisfies a condition.
return false, nil
function funlist:any(predicate)
end
if predicate then
 
for _, v in pairs(mytable) do
-- Public functions
if predicate(v) == true then
-- Determines whether all elements of a sequence satisfy a condition.
return true
function funlist:all(predicate)
end
assert(predicate)
end
for _, v in pairs(self.mytable) do
else
if predicate(v) == false then
for _, _ in pairs(mytable) do
return false
return true
end
end
end
return false
end
end
return true
end


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


    return self
-- 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



Revision as of 16:11, 15 July 2024

Documentation for this module may be created at Module:FunList/doc

local funlist = {}
funlist.__index = funlist

-- Constructor
function funlist.new(tbl)
    assert(type(tbl) == 'table')
    assert(tbl ~= nil)
    
    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
			i = i + 1
			if i == ix then
				return true, v
			end
		end
	end
	
	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
	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
				return true
			end
		end
	else
		for _, _ in pairs(self.mytable) do
			return true
		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 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
	
	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
	return defaultItem
end

-- Returns the first element of a sequence.
function funlist:first()
	for _, v in pairs(self.mytable) do
		return v
	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
	return defaultItem
end

-- Returns the last element of a sequence.
function funlist:last()
	if funlist.any() == false then
		error('Sequence contains no items.')
	end
	local last = nil
	for _, v in pairs(self.mytable) do
		last = v
	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
	local last = nil
	for _, v in pairs(self.mytable) do
		last = v
	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
	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

-- Helper function to check if objects are equal.
function funlist.isEqual(obj1, obj2)
	local type1 = type(obj1)
    local type2 = type(obj2)

    if type1 ~= type2 then
        return false
    end

    if type1 == "number" or type1 == "string" or type1 == "boolean" then
        return obj1 == obj2
    elseif type1 == "table" then
        if #obj1 ~= #obj2 then
            return false
        end
        for k, v in pairs(obj1) do
            if not isEqual(v, obj2[k]) then
                return false
            end
        end
        return true
    else
        return obj1 == obj2
    end
end

return funlist