I have a minor modification for table.insert in Lua. In several places, when I'm working with tables I need code like:
local result = {}
for i=1,N do
local j = key(i)
result[j] = result[j] or {}
table.insert(result[j], value(i))
end
Where key() is a function which returns a key related with element i, and value() is a function which return the result associated with element i. This kind of code allows to insert together all values related with a key.
My modification of table.insert which returns its 1st given table parameter, allowing to write the code as:
local result = {}
for i=1,N do
local j = key(i)
result[j] = table.insert(result[j] or {}, value(i))
end
This re-implementation of table.insert is straightforward, but I'm wondering if the overhead for the ignored result in cases where it is not used will be important or not. As far as I know, ignoring the returned result will be negligible in almost any case.
Am I right, or am I ignoring issues derived from this kind of implementation?