diff --git a/util/util.lua b/util/util.lua index 35ee1ca..d6bd529 100644 --- a/util/util.lua +++ b/util/util.lua @@ -31,6 +31,17 @@ util = { --- 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. -- @tparam table t the list -- @param x the element to remove @@ -61,6 +72,32 @@ util = { return r 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. -- The function receive two argument: the value and then the key. -- @tparam table t initial list