Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

shortened if

Name: Suddenly, Bananas 2013-02-10 14:26

Do any languages allow you to shorten an if statement like this:
if (test == a || test == b || test == c || test == d)
to something like this:
[code]if (test == a || b || c || d)
?

Name: Anonymous 2013-02-10 19:03

>>20
Yeah, the Python compiler's optimizer.  It takes care of things like that.

>>> def f(x,a,b,c,d):
...  if x in [a,b,c,d]:
...   return 'FIOC'
...
>>> dis.dis(f)
  2           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (a)
              6 COMPARE_OP               2 (==)
              9 POP_JUMP_IF_TRUE        48
             12 LOAD_FAST                0 (x)
             15 LOAD_FAST                2 (b)
             18 COMPARE_OP               2 (==)
             21 POP_JUMP_IF_TRUE        48
             24 LOAD_FAST                0 (x)
             27 LOAD_FAST                3 (c)
             30 COMPARE_OP               2 (==)
             33 POP_JUMP_IF_TRUE        48
             36 LOAD_FAST                0 (x)
             39 LOAD_FAST                4 (d)
             42 COMPARE_OP               2 (==)
             45 POP_JUMP_IF_FALSE       52

  3     >>   48 LOAD_CONST               1 ('FIOC')
             51 RETURN_VALUE       
        >>   52 LOAD_CONST               0 (None)
             55 RETURN_VALUE

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List