[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: tri-condition
- From: Mark Edgar <medgar@...>
- Date: Wed, 25 Oct 2006 09:37:18 -0700
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