of     1   

ArbiterOfDeath
#88084311Tuesday, January 29, 2013 11:28 PM GMT

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?
blobbyblob
#88085298Tuesday, January 29, 2013 11:39 PM GMT

Ternary operator. y = true; x = y and "yes" or "no"; print(x); --> yes
blobbyblob
#88085639Tuesday, January 29, 2013 11:43 PM GMT

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").
Parthax
#88085901Tuesday, January 29, 2013 11:45 PM GMT

he could just print out lua.org/pil [Content Trolled]
ArbiterOfDeath
#88086223Tuesday, January 29, 2013 11:48 PM GMT

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!
[rfa#hidefromsearch]
#88086942Tuesday, January 29, 2013 11:56 PM GMT

[rfa#hidefromsearch]
zars15
#88087183Tuesday, January 29, 2013 11:59 PM GMT

^ That, and same goes with ';'
LPGhatguy
Forum Moderator
#88087536Wednesday, January 30, 2013 12:04 AM GMT

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."
awsumpwner27
#88089408Wednesday, January 30, 2013 12:24 AM GMT

I was always confused by this. It looks like it's just a hack.
Tenal
#88090124Wednesday, January 30, 2013 12:32 AM GMT

I either overuse ternaries or just not use it at all. Depending on my mood.
[rfa#hidefromsearch]
#88090351Wednesday, January 30, 2013 12:34 AM GMT

[rfa#hidefromsearch]
ArbiterOfDeath
#88090725Wednesday, January 30, 2013 12:39 AM GMT

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.
ArbiterOfDeath
#88090957Wednesday, January 30, 2013 12:41 AM GMT

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.
BlueTaslem
#88091476Wednesday, January 30, 2013 12:47 AM GMT

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.
BlueTaslem
#88091524Wednesday, January 30, 2013 12:47 AM GMT

Okay, ignore the random block of code at the top of the screen, I forgot that was on the clipboard. That is completely irrelevant.
BlueTaslem
#88091750Wednesday, January 30, 2013 12:50 AM GMT

It appears on further testing that the ternary function is about 3 to 4% slower than the 'if' function.
[rfa#hidefromsearch]
#88092179Wednesday, January 30, 2013 12:55 AM GMT

[rfa#hidefromsearch]
BlueTaslem
#88092446Wednesday, January 30, 2013 12:58 AM GMT

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.

    of     1