Module:FunList: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 31: Line 31:
-- Public functions
-- Public functions
-- Determines whether all elements of a sequence satisfy a condition.
-- Determines whether all elements of a sequence satisfy a condition.
function funlist.all(predicate)
function funlist:all(predicate)
assert(predicate)
assert(predicate)
for _, v in pairs(mytable) do
for _, v in pairs(mytable) do
Line 42: Line 42:
-- Determines whether any element of a sequence exists or satisfies a condition.
-- Determines whether any element of a sequence exists or satisfies a condition.
function funlist.any(predicate)
function funlist:any(predicate)
if predicate then
if predicate then
for _, v in pairs(mytable) do
for _, v in pairs(mytable) do
Line 58: Line 58:


-- Appends a value to the end of the sequence.
-- Appends a value to the end of the sequence.
function funlist.append(item)
function funlist:append(item)
assert(item)
assert(item)
table.insert(mytable, item)
table.insert(mytable, item)
Line 64: Line 64:
-- Determines whether a sequence contains a specified element.
-- Determines whether a sequence contains a specified element.
function funlist.contains(item)
function funlist:contains(item)
assert(item)
assert(item)
for _, v in pairs(mytable) do
for _, v in pairs(mytable) do
Line 75: Line 75:
-- Returns the number of elements in a sequence.
-- Returns the number of elements in a sequence.
function funlist.count()
function funlist:count()
local count = 0
local count = 0
for _, _ in pairs(mytable) do
for _, _ in pairs(mytable) do
Line 84: Line 84:
-- 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(index)
if found == true then
if found == true then
Line 94: Line 94:
-- 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(index)
if found == true then
if found == true then
Line 111: Line 111:
-- Returns the first element of a sequence, or a default value if no element is found.
-- Returns the first element of a sequence, or a default value if no element is found.
function funlist.firstOrDefault(defaultItem)
function funlist:firstOrDefault(defaultItem)
for _, v in pairs(mytable) do
for _, v in pairs(mytable) do
return v
return v
Line 131: Line 131:
-- Returns the last element of a sequence, or a default value if no element is found.
-- Returns the last element of a sequence, or a default value if no element is found.
function funlist.lastOrDefault(defaultItem)
function funlist:lastOrDefault(defaultItem)
if funlist.any() == false then
if funlist.any() == false then
return defaultItem
return defaultItem

Revision as of 15:45, 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)
	
	-- Private fields
	local mytable = tbl
	
	-- 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(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(mytable) do
				if predicate(v) == true then
					return true
				end
			end
		else
			for _, _ in pairs(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(mytable, item)
	end
	
	-- 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
			end
		end
		return false
	end
	
	-- Returns the number of elements in a sequence.
	function funlist:count()
		local count = 0
		for _, _ in pairs(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(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(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(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(mytable) do
			last = v
		end
		return last
	end
	
    return self
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