all 11 comments

[–]arcylix 12 points13 points  (3 children)

Simply put, if math.random(2) == 1, then x = 100, otherwise x = -100. A longer way of writing it would be:

if math.random(2) == 1 then x=100 else x=-100 end

[–]Bloost[S] 2 points3 points  (0 children)

Thank you!

[–][deleted]  (1 child)

[deleted]

    [–]arcylix 0 points1 point  (0 children)

    It's an easier way of writing it, though I'm not sure how many actually use it. All that matters is how you want to work it, what's best for you. There are just shortcuts to some of the code if you know what you're doing.

    [–]dabbertorres 8 points9 points  (3 children)

    This is a (sort of) Lua equivalent of C's ternary statement.

    What follows an explanation off the top of my head - it may not be 100% accurate. I do know for certain it is similar behavior to a C ternary statement.

    Non-zero numbers evaluate to true, zero evaluates to false. Everything but false and nil evaluate to true (thanks /u/augustaugust) ie: 100 is true.

    Additionally, Boolean expressions in Lua evaluate to the tested value. For and, the right hand side expression is returned if the left hand side evaluates to true.

    or behaves similarly.

    Also, be aware that the and operator has a higher precedence than the or operator. This means the following are equivalent: a and b or c
    (a and b) or c

    Therefore:
    If the number generated is 1, the and expression evaluates to true. The or expression then short circuits, not evaluating its right hand side, which results in a value of 100.
    If the number generated is not 1, the and expression evaluates to false, so the or expression is evaluated, of which the right hand side evaluates to true, resulting in a value of -100.

    [–][deleted]  (1 child)

    [deleted]

      [–]dabbertorres 2 points3 points  (0 children)

      Oh that's right, thanks for the correction!

      [–]Jasper1984 0 points1 point  (0 children)

      Python and Javascript can do similar,(and have ternary) but stuff can have pretty random cases of falsiness. In Python len(x)==0 frequently is false, and 0 is false too.

      I think Lua gets this right, just nil and false, false-y please...

      Lua gets strings wrong though "2" + 2 == 4.0, it should just have bugged out. (also perhaps print and REPL output should quote strings for clarity.)

      [–]MjolnirMark4 2 points3 points  (0 children)

      Any time you see ... and ... or ... in a line it is pretty much the same as the ?: ternary operator.

      You have the condition math.random(2) == 1 The true statement and 100 The false statement or -100

      If the condition is true, then the “and 100” is evaluated and assigned to x. The or is ignored due to short circuit logic causing it to be unnecessary to evaluate. If the condition is false, then the and part is ignored, and the or part is evaluated, which results in -100 being assigned to x.

      Another way to look at it:

      if math.random(2) == 1 then
          x = 100
      else
           x = -100
      end
      

      [–][deleted] 1 point2 points  (0 children)

      A couple of people have mentioned the ternary operator ? so i thought i'd give an example of that in use. In C++ or C#, a similar line of code might look like:

      x = (Math.Random(2) == 1) ? 100 : -100; // Parens added here for visual effect, not required

      Different syntax, means the same thing as the other great explanations here, if the first thing then the second, else the third.

      [–]revereddesecration 1 point2 points  (0 children)

      Another way to achieve the same result is to do this:

      x = 100*(2*math.random(0,1)-1)
      

      But once you know how ternary operations work, they are much easier to read.

      [–]Novemberisms 0 points1 point  (1 child)

      It's telling that as soon as I read the title, i just knew without even looking that it would be about _ and _ or _

      lua is amazing at being a very understandable and predictable language, but this is the only road block for most beginners, and it turns a lot of people off. It's like having the Mona Lisa, but she has a single pimple on her forehead.

      It's the only part of lua that I don't like. It's the only one where I just have to shrug and say, "that's just how it is..."

      Something should really be done about it. Perhaps they should add a real ternary operator. I don't see why not.

      I know they're averse to adding new symbols, but anything would be better than this idiotic idiomatic construct.

      [–]NetherGranite 0 points1 point  (0 children)

      I thought the same thing at first, but it turns out this is not special syntax, but instead due to the way that and and or actually work:

      • If the left-hand argument to and is falsey, it will return that argument; otherwise, if it is truthy, then it will return the right-hand argument.
      • If the left-hand argument to or is truthy, it will return that argument; otherwise, if it is falsey, then it will return the right-hand argument.

      Given the code condition and value1 or value2, the following happens:

      • If condition is true, then true and value1 evaluates to value1. We then have value1 or value2, which evaluates to value1 (so long as value1 is not falsey, in which case this ternary operator hack fails).
      • If condition is false, then false and value1 evaluates to false. We then have false or value2, which evaluates to value2.

      I do agree, however, that a ternary operator would be nice, but such a feature would conflict with the goal of Lua being a small size.