HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Some table_handling tools for lua

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
table_handlingforsometoolslua

Problem

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.i

Solution

What is truthmap trying to achieve? I can't quite figure it out, sorry.

contains by nature will always be O(N) in your implementation, so yes, it's speed is equal to the number of elements in your table.

count should probably be renamed to countOf to better signify it's intent. Better yet, you could rename it where, have it take a function, and then allow the user to use the standard # operator on the resultant table.

removePerValue seems unnecessary with a where function. Users can simply filter the table rather than remove values from the table.

equalsAll in my opinion again should take a function as an argument and not an object.

You should change your loops from for k, v in pairs(tbl) do to for _, v in pairs(tbl) do if you are not using k.

I'm sure there's things to be said about the implementation of equalsAll but I don't have time to point that out at the moment.

Do you know about the # operator? appending # to the front of a table will return a value of the size of the table (more precisely, it will return the highest indexed number in the table, so it will be incorrect for sparse arrays).

You should ideally not need comments to explain the purpose of code - your functions should describe themselves ;-)

Context

StackExchange Code Review Q#62394, answer score: 5

Revisions (0)

No revisions yet.