don't rely on shallow copy to construct new classes. also multiple ducks

This commit is contained in:
2025-02-15 17:25:27 +03:00
parent d4c79731b0
commit 954277688f
5 changed files with 110 additions and 59 deletions

View File

@ -55,10 +55,11 @@ function List:__tostring()
return s
end
---Appends v to the end of the list.
---@param v any
function List:push(v)
table.insert(self, v)
---Appends value to the end of the list.
---@param value any
function List:push(value)
if self:has(value) then return end
table.insert(self, value)
end
---Removes the last element in the list and returns it.
@ -82,6 +83,13 @@ function List:filter(predicate)
return filter(self, predicate)
end
---Removes the value at the given index.
---@param idx integer
---@return any
function List:remove_at(idx)
return table.remove(self, idx)
end
---Returns the index of value, if it exists in the list, -1 otherwise.
---@param value any
---@return integer
@ -95,4 +103,27 @@ function List:find(value)
end
return idx
end
---Returns true if the value exists in the list.
---@param value any
---@return boolean
function List:has(value)
return self:find(value) ~= -1
end
---Removes the value from the list, if it exists.
---@param value any
function List:remove_value(value)
local idx = self:find(value)
if idx ~= -1 then
table.remove(self, idx)
end
end
---Returns true if the list is empty.
---@return boolean
function List:is_empty()
return #self == 0
end
return List

View File

@ -1,10 +1,19 @@
---@class Signal
local Signal = {
_connections = {}
}
---@field private _connections table
local Signal = {}
Signal.__index = Signal
---Constructs a new signal.
---@return Signal
function Signal.new()
local s = {
_connections = {},
}
return setmetatable(s, Signal)
end
---Connects f to this signal. When the signal is emmitted, this function will be called.
---@param f function
function Signal:connect(f)
@ -26,14 +35,4 @@ function Signal:emit(...)
end
end
---Constructs a new signal.
---@return Signal
function Signal.new()
local s = {
_connections = {},
}
return setmetatable(s, Signal)
end
return Signal