I'm writing some table.tools to make table handling a little easier for me. But I'm a beginner with Lua, so you might help me a bit, to make the code better.
First of all I would like to get the best usability, but performance is a factor as well.
-- is compare equal to AT_LEASE one entry of list_to_search_in?
-- the idea was to make syntax like
--" if value == var1 or value == var2 or value == var(n) "
--- a little shorter
function table.contains(list_to_search_in, compare)
for k,v in pairs(list_to_search_in) do
if compare == v then
return true
end
end
return false
end
-- is compare equal to EVERY entry of list_to_search_in ?
function table.equalsAll(list_to_search_in, compare)
local counter = 0
local truth_counter = 0
for k,v in pairs(list_to_search_in) do
if compare == v then
truth_counter = truth_counter + 1
end
counter = counter + 1
end
return (truth_counter == counter)
end
-- compare is equal to HOW_MANY entrys of list_to_search_in?
-- returns how many entrys are the same
function table.count(list_to_search_in, compare)
local counter = 0
for k,v in pairs(list_to_search_in) do
if compare == v then
counter = counter + 1
end
end
return counter
end
-- compare is equal to WHICH entrys of list_to_search_in?
-- returns a map of equality
function table.truthmap(list_to_search_in, compare)
local map = { }
for k,v in pairs(list_to_search_in) do
table.insert(map, { k , compare == v})
end
return map
end
-- deletes all entys in list, wich equals(or not[,invert]) to compare value
-- returns a table with all entrys wich have not been equal
function table.removePerValue(list_to_search_in, compare, invert)
local counter = 0
local return_table = {}
for k,v in pairs(list_to_search_in) do
if invert and (v == compare) then
table.insert(return_table, v)
elseif not invert and (v ~= compare) then
table.insert(return_table, v)
end
end
return return_table
end
(Feel free to use, I'm happy if the code helps someone)
removePerValue
does? \$\endgroup\$