|
Hey guys, I have been working on a book on RBX Lua and RBX userdatas and I'm just finishing up the outline; one of my subtopics is the use of, "not," "and," and "or," as a replacement for if statements. I know that there is a formal name for this type of manipulation, but I can't recall it at the moment. Any google searches for, "pseudo ifs," just yields results on pseudo-code. I think the term started with a P but I'm not sure. Any hints? |
|
|
Ternary operator.
y = true;
x = y and "yes" or "no";
print(x); --> yes |
|
|
You can actually mess around a lot with it. You might want to look into "short circuit evaluation" which allows you to do things like this:
local humanoid = (hit.Parent and hit.Parent:findFirstChild("Humanoid")) or (hit.Parent and hit.Parent.Parent and hit.Parent.Parent:findFirstChild("Humanoid"));
This example would get the humanoid if 'hit' was a child of the character model, or a child's child (eg, a hat block or tool).
Or a more simple example.
x = y or "nil";
(if y is false or nil, x is assigned the string "nil"). |
|
ParthaxJoin Date: 2011-04-27 Post Count: 6941 |
he could just print out lua.org/pil
[Content Trolled] |
|
|
Nice, and thanks. But eh... you know that you don't need the parenthesis there because
and binds more tightly then or in lua? Just a helpful suggestion. Thanks for the help! |
|
|
zars15Join Date: 2008-11-10 Post Count: 9999 |
^
That, and same goes with ';' |
|
LPGhatguyForum ModeratorJoin Date: 2008-06-27 Post Count: 4725 |
Just don't get too carried away with it, like in one of my old event methods:
event.event_get_priority = function(priority, object, event_name)
return ((type(priority) == "table") and priority[event_name]) or
((type(priority) == "number") and priority) or
(type(object) == "table") and (object["event_priority"] and object["event_priority"][event_name])
end
To which Stravant simply replied:
"Don't do that sort of thing, please." |
|
|
I was always confused by this.
It looks like it's just a hack. |
|
TenalJoin Date: 2011-05-15 Post Count: 18684 |
I either overuse ternaries or just not use it at all. Depending on my mood. |
|
|
|
I only use it when I have a single if statement and I don't feel like useing 3 or 5 lines to do something simple >.<
On a side note, Has anyone researched their speed in lua? I might want to compare it to normal ifs. |
|
|
I consider it to be faster because it isn't setting up a new enviroment level then closing it again; I have never actually tested it. |
|
|
My prediction is slower ternary is slower, though 'if' tends to be really slow seemingly.
Here will be the test functions:
--[[function go(px,inch)
local scr = 320; --pixels
local fov = 50; --from left to right, deg.
-- -- -- -- -- --
local theta = math.rad(fov * px / scr);
return (inch/2) / math.tan( theta / 2 );
end
--pixels on screen, actual width of target left to right
print(go(102,2.25) ..'"');
--164,3.5 --> 7.7" | 6.25"
--180,2 --> 4" | 4"
--163,2.5 --> 5.5 | 5.25
--79, 2.75 --> 12.71 | 12.75
--102,2.25 --> 8.0" | 8"]]
function A()
local r = nil;
if math.random() < 0.5 then
r = math.random();
else
r = math.random();
end
return r;
end
function B()
local r = nil;
r = (math.random() < 0.5) and math.random() or math.random();
return r;
end
function time(f,r)
local k = os.clock();
for i = 1,r do
f()
end
print(os.clock() - k);
end
print("B, ternary");
time(B,10000000);
print("A, if");
time(A,10000000);
--[[Run 1
A, if
6.021
B, ternary
4.607
]]
--[[Run 2
A, if
5.38
B, ternary
4.914
]]
--[[Run 3
B, ternary
6.058
A, if
6.525
]]
--[[Run 4
B, ternary
5.186
A, if
4.379
]]
So, apparently ternary is faster. I will refine this data.
That said, not all if statements can be rewritten like this. |
|
|
Okay, ignore the random block of code at the top of the screen, I forgot that was on the clipboard. That is completely irrelevant. |
|
|
It appears on further testing that the ternary function is about 3 to 4% slower than the 'if' function. |
|
|
|
Probably not significantly.
Another thing I noted was that there was only a significant speed difference when the number of iterations run was very very high, so it's possible under normal circumstances they aren't really different in speed at all. |
|