lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


askok@dnainternet.net wrote:
Took a new approach to the ? : dilemma, this solution is using Lua scripts only.

You could avoid closure creation:

local function true_(tbl) return tbl[1] end
local function false_(tbl) return tbl[2] end
function tf(cond)
  if cond then return true_
  else         return false_
  end
end
assert( tf(true) { 'a','b' } == 'a'  )
assert( tf(nil) { 'a','b' } == 'b'  )


By changing the syntax slightly, you can also avoid table creation:

local function true_(t,f) return t end
local function false_(t,f) return f end
local function tf(cond)
  if cond then return true_
  else         return false_
  end
end
assert( tf(true) ( 'a','b' ) == 'a'  )
assert( tf(nil) ( 'a','b' ) == 'b'  )


Personally, the standard Lua idiom works well for me:

assert( ( true and 'a' or 'b' ) == 'a' )
assert( ( nil and 'a' or 'b' ) == 'b' )

					-Mark