lua-users home
lua-l archive

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



Took a new approach to the ? : dilemma, this solution is using Lua scripts only.

No comments needed, if you like it, use it.

I will be checking for a "? cond { truechoice, falsechoice }" syntax, using token filtering.

-asko


--
-- TRICOND.LUA Copyright (c) 2006, Asko Kauppi
--
-- Like C/Perl "cond ? a : b" operator, done using Lua scripts.
--
-- Usage:   require "tricond"
--
--          a= tf(cond) { "either, "or" }
--
-- The approach is very switch-like, only here any logically true Lua expression -- leads to the first [1] table entry, and any false (or nil) to [2].
--
-- Author:  <akauppi@gmail.com>
--
--[[

-----
-- tf(cond) { true_choice, false_choice }
--
function tf( cond )
  return function(tbl)
assert( tbl, "Usage: tf(cond) { true_choice, false_choice }" )
            assert( tbl[1], "No true choice!" )
            assert( tbl[2], "No false choice!" )

            if cond then return tbl[1]
            else         return tbl[2]
            end
         end
end

assert( tf(true) { 'a','b' } == 'a'  )
assert( tf(nil) { 'a','b' } == 'b'  )