Jump to content

Need evaluation on switch statement utility


Zeros

Recommended Posts

Hi, I need someone to evaluate which piece of code should used on rendering call. To be honest, I'm thinking of scrapping the FIRST VERSION because it's recreating table and closure. Worst case is recreating the closure on each call. I might be wrong about this hence I need someone to point out what I'm missing. I would appreciate it if there's better idea to improve the first version because variation one coding style is much more familiar.

 

local validate = function(t)
  if (not t) then
    error("Switch: Argument is nil", 2)
  elseif (type(t) ~= "table") then
    error("Switch: Argument is not a table", 2)
  end
end

-- First Version
local switch = function(val)
  return function(t)
    validate(t)
    if t[val] then
      return t[val]()
    else
      return t["default"]()
    end
  end
end

-- Variation 1
local function render(...)
  local i = 10
  switch(i) {
    [1] = function()
      print("One")
    end,
    [10] = function()
      print("Ten")
    end,
    default = function()
      print("Unknown value!")
    end,
  }
end

-- Variation 2
local tSwitch = {}
local function render(...)
  local i = 10
  tSwitch.Value = tSwitch.Value or {
    [1] = function()
      print("One")
    end,
    [10] = function()
      print("Ten")
    end,
    default = function()
      print("Unknown value!")
    end,
  }
  switch(i)(tSwitch.Value)
end

-- Second Version
local switch = function(val, t)
  validate(t)
  if t[val] then
    return t[val]()
  else
    return t["default"]()
  end
end

local tSwitch = {}
local function render(...)
  local i = 10
  tSwitch.Value = tSwitch.Value or {
    [1] = function()
      print("One")
    end,
    [10] = function()
      print("Ten")
    end,
    default = function()
      print("Unknown value!")
    end,
  }
  switch(i, tSwitch.Value) 
end

 

Edited by Zeros
Link to comment
Share on other sites

I encountered a critical issue when using the second version. Since the switch function is defined once without getting values from the table function arguments, it will use the very first upvalue, meaning that the function will not receive any new data. Therefore, I've added variable arguments parameter to the switch function.

 

local validate = function(t)
	if (not t) then
		error("Switch: Argument is nil", 2)
	elseif (type(t) ~= "table") then
		error("Switch: Argument is not a table", 2)
	end
end

local switch = function(val, t, ...)
	validate(t)
	local args = {...}
	if t[val] then
		if (#args > 0) then
			return t[val](unpack(args))
		else
			return t[val]()
		end
	else
		return t["default"]()
	end
end

local tSwitch = {}

-- Example One - Without Arguments
do
  tSwitch.Value = tSwitch.Value or {
    [1] = function()
      print("One")
    end,
    [2] = function()
      print("Two")
    end,
    [3] = function()
      print("Three")
    end,
    [4] = function()
      print("Four")
    end,
    [5] = function()
      print("Five")
    end,
    default = function()
      print("Unknown value!")
    end,
  }
  for i=1, 6 do
    switch(i, tSwitch.Value)
  end
end

--Example Two - With Arguments
do
  tSwitch.Var = tSwitch.Var or {
    ["add"] = function(i1, i2)
      print(i1 + i2)
    end,
  }
  switch("add", tSwitch.Var, 5, 10)
end

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...