Trinocular operation

Old drivers who are familiar with C / C ++ know trinocular operation a? B: c. This kind of writing replaces simple conditional judgment statements to keep the code as simple as possible without increasing the difficulty of reading.

int a, b, ret;

// if-else

if (a> b)

ret = a;

else

ret = b;

// Three-eye operator

ret = a> b? a: b;

Trinocular operations in Lua

Lua's native semantics does not implement trinocular operations. Generally, the trinocular operators are simulated by the logical operators and and or.

In Lua, both and and or use "short_cur evaluation", that is, they only evaluate the second operand when needed. ("Lua Programming")

local a, b, ret;

ret = a> b and a or b

Exhausting all possibilities:

When a> b:

a> b and a-> true

a or b-> a

When a b and a-> false

a or b-> b

perfect!

Trinary operator traps in Lua

According to the principle from special to general:

General form of trinocular operation a? B: c

a = true, the result is b

a = false, the result is c

Corresponds to a and b or c in Lua

b = true

a = true

a and b-> true

b or c-> b

a = false

a and b-> false

b or c-> c

b = false

a = true

a and b-> false

b or c-> c

a = false

a and b-> false

b or c-> c

It can be seen that when b = false, a and b or c simulated by Lua always returns c and cannot restore the original appearance of the trinocular operator.

"Lua Programming" also recommends using if-else to avoid this situation.

Generalized a and b or c

So is there a way to solve the problem of b = false invalidation?

From this I think of C as a general test question: Please use a macro definition to write a method that returns the smaller of the two values.

When I first saw this question in school, my first reaction was as follows:

#define MIN (A, B) A <B? A: B

However, this writing method cannot return correct results in many nested use environments. For example: 2 * MIN (3, 4) expands to 2 * 3 <4? 3: 4 = 6 <4? 3: 4, the result is 4.

More "engineer" is:

#define MIN (A, B) ((A) <(B)? (A): (B))

For curious students, you can refer to: the correct definition of macro definition, macro definition of trinocular operation

From this example, I was wondering how to ensure that b in a and b or c is true or that b is not ambiguous?

The priority of and is higher than or, and simply changing the order of operations is not useful.

At this time, I thought of the universal table in lua. Can you put a, b, and c into the table to change the existence of b? Note that {nil} is also an object that is true.

a, b, c are replaced with table: {a} and {b} or {c}.

In the trinary operation, a is a condition, and the result is b or c. In fact, a does not need to be placed in the table, otherwise {a} will always be true, and the meaning of the condition will be lost. While {b} or

The result of {c} must also be a table, which has only one element. Then you can access it through [1].

In summary, the more general Lua trinocular operation is: (a and {b} or {c}) [1]

to sum up

It is true that if you want to know the form of trinocular operation in Lua, you can find the answer you need by searching. The above reasoning process is somewhat suspect of knowing the answer first, and deliberately relying on the answer. However, I firmly believe that to prove a problem, the conclusion is not the most important, the important thing is the process of demonstration. This article only attempts to reduce the generalization of the Lua trinary operation based on my understanding of Lua. If there is a better reasoning process, we can communicate together.