1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 09:09:30 +00:00

Add has and ipairs to uqt.util

This commit is contained in:
Étienne Fildadut 2021-07-18 19:30:57 +02:00
parent 4b75f21e52
commit 010a526ef3

View file

@ -31,6 +31,17 @@ util = {
--- List operations --- --- List operations ---
----------------------- -----------------------
--- Check if the list contains a value.
-- @tparam table t the list
-- @param v value to search
-- @treturn bool true if is in list, false otherwise
has = function(t, v)
for _, x in ipairs(t) do
if x == v then return true end
end
return false
end,
--- Remove the first occurence of an element in a list. --- Remove the first occurence of an element in a list.
-- @tparam table t the list -- @tparam table t the list
-- @param x the element to remove -- @param x the element to remove
@ -61,6 +72,32 @@ util = {
return r return r
end, end,
--- Chainable ipairs.
-- Same as ipairs, but can take several tables, which will be chained, in order.
ipairs = function(a, b, ...)
if b == nil then
return ipairs(a)
else
local tables = {a, b, ...}
local itable = 1
local f, s, var = ipairs(tables[itable])
return function()
local i, v = f(s, var)
if i == nil then
itable = itable + 1
if tables[itable] then
f, s, var = ipairs(tables[itable])
i, v = f(s, var)
else
return nil
end
end
var = i
return i, v
end
end
end,
--- Applies a function to every item in list t. --- Applies a function to every item in list t.
-- The function receive two argument: the value and then the key. -- The function receive two argument: the value and then the key.
-- @tparam table t initial list -- @tparam table t initial list