why use `not not` operation? - lua

In the following Lua code:
function eq_event(op1, op2)
if op1 == op2 then
return true
end
local h = getequalhandler(op1, op2)
if h then
return not not h(op1, op2)
else
return false
end
end
why use not not before the return value? is it different with the raw return value? I also remember that in C, there also got chance to use the !! before some expression, does them the same?

not not will convert nil into false, and all other values except false to true. You may need to return a boolean value only when interacting with C.
Yes it's the same as !! in C. Only difference is that in C, 0 (and some other values I don't remember) is also falsy (that is, they'll return false when doubled not'd).

Related

Lua __eq on tables with different metatables

I have found the following quote on this site http://lua-users.org/wiki/MetamethodsTutorial:
__eq is called when the == operator is used on two tables, the reference equality check failed, and both tables have the same __eq metamethod (!).
Now I tested it with Lua 5.3.5 and this is not at all what I observed:
a = {}
b = {}
m = {}
m2 = {}
setmetatable(a, m)
setmetatable(b, m2)
m.__eq = function(p1, p2) print("why"); return true end
m2.__eq = function(p1, p2) print("why2"); return true end
This is the code I tested with.
> a == b
why
true
> b == a
why2
true
It looks like it does the same thing as with the comparison operators, where it just takes the left table and uses its metamethod.
Did this change in recent Lua versions or did I make an error with my test?
Thanks for your help.
That changed in Lua 5.3. The readme says it introduced "more flexible rules for some metamethods". Compare the Lua 5.2 reference manual:
the == operation. The function getequalhandler defines how Lua chooses a metamethod for equality. A metamethod is selected only when both values being compared have the same type and the same metamethod for the selected operation, and the values are either tables or full userdata.
function getequalhandler (op1, op2)
if type(op1) ~= type(op2) or
(type(op1) ~= "table" and type(op1) ~= "userdata") then
return nil -- different values
end
local mm1 = metatable(op1).__eq
local mm2 = metatable(op2).__eq
if mm1 == mm2 then return mm1 else return nil end
end
The "eq" event is defined as follows:
function eq_event (op1, op2)
if op1 == op2 then -- primitive equal?
return true -- values are equal
end
-- try metamethod
local h = getequalhandler(op1, op2)
if h then
return not not h(op1, op2)
else
return false
end
end
Note that the result is always a boolean.
With the Lua 5.3 reference manual:
the equal (==) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.

Lua, Error Handling pcall()

local a = {1,2,3,4}
print(pcall(#a[1])) -- still error
Should pcall() return false if error and true if all good?How do I handle errors?
-- Example 1.
a = {1,2,3,4}
function check()
return #a[1]
end
print(pcall(check)) -- false | attempt to get length of field '?' (a number value)
local v, massage = pcall(check)
print(v, massage) -- "v" contains false or true, "massage" contains error string
-- Example 2.
-- Passing function and parameter...
function f(v)
return v + 2
end
a, b = pcall(f, 1)
print(a, b) --> true | 3
a, b = pcall(f, "a")
print(a, b) -- false | attempt to perform arithmetic on local 'v' (a string value)
For pcall() to work, function needs to be passed with out brackets.
The first passed in parameter in pcall is function name, what you have in the example is array, not legal I am afraid
https://www.lua.org/pil/8.4.html

Lua table length function override not working

How does one change the length operator (#) for a table in Lua, the manual suggests assigning the __len function in a metatable and then assigning that metatable to the table I want to override, but this doesn't work as expected? I do not have the option to override this on the C side.
turtles = {1,2,3}
setmetatable(turtles, {__len = function(mytable) return 5 end})
print(#turtles)
--returns 3, should return 5
You must be using Lua 5.1. The __len metamethod on tables is supported since Lua 5.2.
In Lua 5.1 reference manual, if the operand is a table, return the primitive table length directly.
"len": the # operation.
function len_event (op)
if type(op) == "string" then
return strlen(op) -- primitive string length
elseif type(op) == "table" then
return #op -- primitive table length
else
local h = metatable(op).__len
if h then
-- call the handler with the operand
return (h(op))
else -- no handler available: default behavior
error(···)
end
end
end
In Lua 5.2 reference manual, if the operand is a table, check if the __len metamethod is available.
"len": the # operation.
function len_event (op)
if type(op) == "string" then
return strlen(op) -- primitive string length
else
local h = metatable(op).__len
if h then
return (h(op)) -- call handler with the operand
elseif type(op) == "table" then
return #op -- primitive table length
else -- no handler available: error
error(···)
end
end
end

In Lua, how to get the tail of an array without copying it?

I'm wokring with Lua 5.2, and for the sake of this question, assume that the tables are used exclusively as arrays.
Here's a function that returns the tail of an array (the array minus its first element):
function tail(t)
if # t <= 1 then
return nil
end
local newtable = {}
for i, v in ipairs(t) do
if i > 1 then
table.insert(newtable, v)
end
end
return newtable
end
For instance:
prompt> table.concat(tail({10, 23, 8}), ", ")
23, 8
However this is achieved by returning a new copy of the table. Is there a way to avoid the creation of a new table?
I am looking for the equivalent of C's returning a pointer to the next element (t++). Is it possible?
As already explained, this is normally impossible.
However, using metatables, you could implement a tail function that performs what you want without copying all the data, by referencing the original table. The following works for most operations in Lua 5.2, but for example not for table.concat:
function tail(t)
return setmetatable({}, {
__index = function(_, k) return t[k+1] end,
__newindex = function(_, k, v) t[k+1] = v end,
__len = function(_) return #t-1 end,
__ipairs = function(_) return
function(_, i)
if i+1==#t then return nil end
return i+1, t[i+2] end,
t, 0 end,
__pairs = function(t) return ipairs(t) end,
})
end
This is the nicest way I know to implement tail(). It makes one new table, but I don't think that's avoidable.
function tail(list)
return { select(2, unpack(list)) }
end
Nicol is correct that you can't reference a slice of an array, but there is an easier/shorter way to do what you want to do:
function tail(t)
local function helper(head, ...) return #{...} > 0 and {...} or nil end
return helper((table.unpack or unpack)(t))
end
print(table.concat(tail({10, 23, 8}), ", ")) will then print 23,8.
(added table.unpack or unpack to make it also work with Lua 5.2)
I am looking for the equivalent of C's returning a pointer to the next element (t++). Is it possible?
No. The only possible reason you could want this is performance. Such a feature is only found in low-level programming languages. Lua is a scripting language: performance is not such a priority that this would be implemented.
Just make another table like you're doing, or use table.remove to modify the original. Whichever works best for you. Remember: the big, important objects like tables and userdata are all stored by reference in Lua, not by value.
prapin's suggestion, to use metatables to present a view of the sequence, is roughly the way I'd do it. An abstraction that might help is defining a metatable for segments, which can be an 0-ary function that returns a pair of a table and an offset index - we are only using functions here to represent tuples. We can then define a metatable that makes this function behave like a table:
do
local tail_mt = {
__index = function(f, k) local t, i=f(); return t[k+i] end,
__newindex = function(f, k, v) local t,i=f(); t[k+1] = v end,
__len = function(f) local t,i=f(); return #t-i end,
__ipairs = function(f)
local t,i = f ()
return
function (_, j)
if i+j>=#t then
return nil
else
return j+1, t[i+j+1]
end
end, nil, 0
end,
}
tail_mt.__pairs = tail_mt.__ipairs -- prapin collapsed this functionality, so I do too
function tail (t)
if type(t) == "table" then
return setmetatable ( function () return t, 1 end, tail_mt )
elseif type(t) == "function" then
local t1, i = t ()
return setmetatable ( function () return t1, i+1 end, tail_mt )
end
end
end
With __index and __newindex metamethods, you can write code such as f[2]=f[1]+1.
Although this (untested) code doesn't endlessly create one-off metatables, it is probably less efficient than prapin's, since it will be calling thunks (0-ary functions) to get at their contents. But if you might be interested in extending the functionality, say by having more general views on the sequence, I think this is a bit more flexible.

Inline conditions in Lua (a == b ? "yes" : "no")?

Is there anyway to use inline conditions in Lua?
Such as:
print("blah: " .. (a == true ? "blah" : "nahblah"))
Sure:
print("blah: " .. (a and "blah" or "nahblah"))
If the a and t or f doesn't work for you, you can always just create a function:
function ternary ( cond , T , F )
if cond then return T else return F end
end
print("blah: " .. ternary(a == true ,"blah" ,"nahblah"))
of course, then you have the draw back that T and F are always evaluated....
to get around that you need to provide functions to your ternary function, and that can get unwieldy:
function ternary ( cond , T , F , ...)
if cond then return T(...) else return F(...) end
end
print("blah: " .. ternary(a == true ,function() return "blah" end ,function() return "nahblah" end))
You can usually do:
condition and ifTrue or ifFalse
but this isn't necessarily the best way to do it. The major reason why is because if ifTrue is a falsy value (some of the time), ifFalse will evaluate even if condition is a truthy value. One way to do it simply without much extra work is:
(condition and {ifTrue} or {ifFalse})[1]
which has the advantage of not only being an expression and not being subject to the problem of ifTrue being falsy which means it can handle all cases, but also has the advantage of short-circuiting (not evaluating the other expression). No need for extra functions or messing with complex aspects of Lua.
Although this question is fairly very old, I thought it would be fair to suggest another alternative that syntactically appears very similar to that of the ternary operator.
Add this:
function register(...)
local args = {...}
for i = 1, select('#', ...) do
debug.setmetatable(args[i], {
__call = function(condition, valueOnTrue, valueOnFalse)
if condition then
return valueOnTrue
else
return valueOnFalse
end
end
})
end
end
-- Register the required types (nil, boolean, number, string)
register(nil, true, 0, '')
And then use it like this:
print((true) (false, true)) -- Prints 'false'
print((false) (false, true)) -- Prints 'true'
print((nil) (true, false)) -- Prints 'false'
print((0) (true, false)) -- Prints 'true'
print(('') (true, false)) -- Prints 'true'
Note: For tables, however, you cannot use them directly with the above method. This is because each and every table has it's own independent metatable and Lua does not allow you to modify all tables at once.
In our case, an easy solution would be to convert the table into a boolean using the not not trick:
print((not not {}) (true, false)) -- Prints 'true'
You could just write the if statement in one line, it is no shorthand, inline or ternary operator stuff tho.
if (dummy) then
print("dummy is true")
end
is equal too
if (dummy) then print("dummy is true") end
Have fun
:D
local n = 12
do
local x = (n>15)
and print(">15")
or n>13
and print(">13")
or n>5
and print(">5")
end

Resources