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

Return table value based on a primitive "priority queue"

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

Problem

I have 2 function in Lua, one to look if a key is present inside a provided table and if true then return the value, else return false. This lookup function is called by another function with a "priority queue". It is not a queue per definition but I don't have a better name for it.

function inTable(tbl, item)
    for key, value in pairs(tbl) do
        if key == item then 
          return value 
        end
    end
    return false
end

function ReturnString()
local valid = {}

valid[22] = "string1"
valid[13] = "string2"
valid[25] = "string3"
...

  if inTable(valid, "22") then
    return inTable(valid, "22")
  elseif inTable(valid, "13") then
    return inTable(valid, "13")
  elseif inTable(valid, "25") then
    return inTable(valid, "25")
  else
    print("nope not found")
  end
end


Usage:

print(ReturnString())


valid is filled with a lot of different key-value pairs, unordered. My goal is to return the value based on a priority I determine using my nested if-elseif-else construct. If there is a key 22 then return string1, else check if there is a key 13 and so on. I know that there are more real implementation of a priority queue like this but I prefer something simple. What improvement can I do without implementing a complex priority queue?

Solution

welcome to Code Review.

I don't understand the purpose of your inTable() function. Lua tables already support immediate lookup of entries in them, just by using square brackets. See the Lua documentation for tables.

So your code:

if inTable(valid, "22") then
    return inTable(valid, "22")
  elseif inTable(valid, "13") then
    return inTable(valid, "13")
  elseif inTable(valid, "25") then
    return inTable(valid, "25)


is really equivalent to:

if valid[22] then
  return valid[22]
elseif valid[13] then
  return valid[13]
elseif valid[25] then
  return valid[25]


Can we do better than this? Sure! Lua has the or operator which returns the first true value. (See documentation)

return valid[22] or valid[13] or valid[25]


Can we do even better? Definitely. Make up another table with the indexes in order, then iterate over it, returning the first true value.

local priority = {22, 13, 25}
local i
for i in priority do
   if valid[i] return valid[i]
end

Code Snippets

if inTable(valid, "22") then
    return inTable(valid, "22")
  elseif inTable(valid, "13") then
    return inTable(valid, "13")
  elseif inTable(valid, "25") then
    return inTable(valid, "25)
if valid[22] then
  return valid[22]
elseif valid[13] then
  return valid[13]
elseif valid[25] then
  return valid[25]
return valid[22] or valid[13] or valid[25]
local priority = {22, 13, 25}
local i
for i in priority do
   if valid[i] return valid[i]
end

Context

StackExchange Code Review Q#90156, answer score: 3

Revisions (0)

No revisions yet.