Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"or" operator without repeating the left hand condition again

Ever since I started programming properly using good old VB6 and up until present day, I often still get burned (and just have) by this in programming:

if x == something or x == somethingelse

I often end up writing:

if x == something or somethingelse

Just out of pure interest, does any langauge/languages out there support this?

like image 957
Finglas Avatar asked Jan 14 '10 18:01

Finglas


1 Answers

Python does, kind of:

if x in [something, somethingelse]:
    ...

in simply checks whether an element occurs in a given list. Similarly, in Haskell:

if x `elem` [something, somethingelse] then ...

I suppose that this can be done in most languages that allow for expressions of list type.

like image 168
Thomas Avatar answered Oct 13 '22 22:10

Thomas