views:

1857

answers:

22

Possible Duplicate:
Why do most programming languages only have binary equality comparison operators?

I have had a simple question for a fairly long time--since I started learning programming languages.

I'd like to write like "if x is either 1 or 2 => TRUE (otherwise FALSE)."

But when I write it in a programming language, say in C,

( x == 1 || x == 2 )

it really works but looks awkward and hard to read. I guess it should be possible to simplify such an or operation, and so if you have any idea, please tell me. Thanks, Nathan

+30  A: 

Python allows test for membership in a sequence:

if x in (1, 2):
Ignacio Vazquez-Abrams
Wow, it works! But is it normal to use an array?
That's not an array, it's a structure called a **tuple**. And it is normal to use one for up to 5 or so values; beyond that it's more efficient to use a **set** (containment testing in a sequence is O(n), whereas containment testing in a set is O(1)).
Ignacio Vazquez-Abrams
I see. I learned many great things from you today. Thank you!
One of the many examples showing python gives you beautiful code compared to some of the more traditional languages :).
bastijn
@Ignacio Vazquez-Abrams: how can it be O(1) with sets? We were taught it'll be O(log n) at best...
progo
@progo: Because sets are represented as hashtables, which have constant average lookup time.
Matajon
@Matajon: oh, Python never ceases to amaze me :)
progo
Well in Python a tuple is just an immutable list.
KennyTM
But wouldn't creating the set be O(n)?
configurator
So if I wrote a piece of code (forgive me for not knowing the set syntax) `if x is set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10):`, it would create the set every time it got to that line, so a tuple would have been quicker anyway. At least so it seems to me.
configurator
You create the set ahead of time, and refer to it by name each time it's needed.
Ignacio Vazquez-Abrams
Same kind of syntax in Pascal, plus you can have either a set (1, 2, 3) or an interval (1..3). That was one of the major letdowns of C for me when learning it after Pascal, the power of switch/case, versus case/of, which in Pascal allows testing in sets and intervals.
jv42
I don't know any python, but I suppose it's O(1) on *average*, if you test many values over a constant set. If you're testing a single value it's still O(n) because you have to create the set...
Kobi
@configurator: In Python 3, you could use a set by writing `if x in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}`.
endolith
Kobi: The access is very close to O(1) even when testing a single value - but the cost of creating the set is ignored so very often.
configurator
Hash-table lookup isn't O(1), it's O(k); i.e. it depends on the particular hash-table and the hash function rather than number of items in the table.
Roger Pate
@Roger: Good point.
configurator
If the Python compiler was smart it would build a constant set once during compile/parse and keep it around forever. However, is it that smart?
Zan Lynx
@Zan: It can't know whether `set` refers to `__builtins__.set` or some module/local object, so it does the call at runtime.
Ignacio Vazquez-Abrams
+2  A: 

Thanks Ignacio! I translate it into Ruby:

[ 1, 2 ].include?( x )

and it also works, but I'm not sure whether it'd look clear & normal. If you know about Ruby, please advise. Also if anybody knows how to write this in C, please tell me. Thanks. -Nathan

Culip
For C you create an array with a sentinel at the end and you iterate through the array looking for either a match or the sentinel. If the resultant index contains the sentinel then return false, otherwise return true.
Ignacio Vazquez-Abrams
That's completely unreadable!
UpTheCreek
@UpTheCreek: Welcome to Ruby. Power of Python, legibility of Perl.
Ignacio Vazquez-Abrams
I probably wouldn't do it this way for only two numbers but it's certainly not unreadable for a Ruby programmer.
Jonas Elfström
+3  A: 

In php you can use

$ret = in_array($x, array(1, 2));

Joyce Babu
Holy moly: a memory allocation, object construction, function call, linear search, destruction, and garbage collect to do something equivalent to two compare-and-branch opcodes. PHP fulfills all my expectations yet again.
Crashworks
@Crashworks Surprising last sentence from the likes of a gentleman of such highness as yourself.
Yehonatan
+3  A: 

As far as I know, there is no built-in way of doing this in C. You could add your own inline function for scanning an array of ints for values equal to x....

Like so:

inline int contains(int[] set, int n, int x)
{ 
  int i;
  for(i=0; i<n; i++)
    if(set[i] == x)
      return 1;

  return 0;
}

// To implement the check, you declare the set
int mySet[2] = {1,2};
// And evaluate like this:
contains(mySet,2,x) // returns non-zero if 'x' is contained in 'mySet'
S.C. Madsen
+6  A: 

Err, what's wrong with it? Oh well, if you really use it a lot and hate the looks do something like this in c#:

#region minimizethisandneveropen
public bool either(value,x,y){       
    return (value == x || value == y);
}
#endregion

and in places where you use it:

if(either(value,1,2))
   //yaddayadda 

Or something like that in another language :).

bastijn
You could avoid the condition in your function: `return (value == x || value == y);`
dark_charlie
woops, so true haha. Its early ;)
bastijn
A small comment, if I may - this is a little confusing. I'd expect `value` to go first (but that's just me), and `x` is used twice, once as `value` and once as the first argument.
Kobi
@dark_charlie: Aarh, I'm unable to think how :( Please hint me!
legends2k
well, it was just to show the idea, but I fixed them for you :).
bastijn
+17  A: 

While there are a number of quite interesting answers in this thread, I would like to point out that they may have performance implications if you're doing this kind of logic inside of a loop depending on the language. A far as for the computer to understand, the if (x == 1 || x == 2) is by far the easiest to understand and optimize when it's compiled into machine code.

Darth Android
But this depends on the language. I would expect the code generated by Python for `x in (1, 2)` to be at least as well optimized as that generated by Python for `x == 1 or x == 2` Of course in C the equivalent opposite is true.
intuited
True, but as mentioned by intuited this would come down to the underlying implementation. But I don't think that answers the OP's question, even if it's true and worth mentioning.
haylem
@intuited - just FYI: In Python 3.1.2, the performance for both is nearly the same. I just `time` d it, and I get 0.042 seconds for `in` and 0.047 seconds for `or` if `x` is 1, and 0.08 seconds for both if `x` is 2 or 0. So if you're doing microoptimization in Python, look at the order of the conditions you're testing...
Tim Pietzcker
+2  A: 

Perl 5 with Perl6::Junction:

use Perl6::Junction 'any';
say 'yes' if 2 == any(qw/1 2 3/);

Perl 6:

say 'yes' if 2 == 1|2|3;

This version is so readable and concise I’d use it instead of the || operator.

zoul
A: 

return x === 1 || x === 2 in javascript

Amareswar
Not sure why this has been down-voted, though some more explanation would help. In this case the use of `===` would make sure that `1` or `2` passed, but that `"1"` or `"2"` would fail, while `==` would implicitly covert the strings and compare the values. You could want either way of doing it in a dynamically typed language.
Keith
This doesn't really answer the question at all.
Paddy
I think it's downvoted because it doesn't answer the question at all. The OP doesn't want to know about ways to do the equivalent statement in other languages, but ways to write it more expressively.
haylem
+3  A: 

In .Net you can use Linq:

int[] wanted = new int{1, 2};

// you can use Any to return true for the first item in the list that passes
bool result = wanted.Any( i => i == x );

// or use Contains
bool result = wanted.Contains( x );

Although personally I think the basic || is simple enough:

bool result = ( x == 1 || x == 2 );
Keith
does not compile....
jdv
You probably mean `Contains` `:)`
Kobi
@Kobi - cheers, yes I do.
Keith
Oooooh, you changed the code without updating the comment. Naughty =)
demoncodemonkey
+8  A: 

that was a lot of interesting alternatives. I am surprised nobody mentioned switch...case - so here goes:

switch(x) {
case 1:
case 2:
  // do your work
  break;
default:
  // the else part
}
  1. it is more readable than having a bunch of x == 1 || x == 2 || ...
  2. more optimal than having a array/set/list for doing a membership check
kartheek
I don't agree that it looks more readable. x==1 || x==2 is pretty straight forward compared to a switch in my opinion. It may be more optimal than an array check, but it's probably not more optimal than the original statement.
Patrick
it may not be that obvious when you have two comparisons, but when you have say 10 comparisons then doing a x == 1 || ... x == 10 can get overwhelming and error prone also. although you are comparing the same var "x" you repeat it 10 times - and you need to go through all 10 of htem to make sure one of them is not a "y"
kartheek
Then I would still definitely prefer an implementation iterating over an array instead of a switch case, which is going to take a lot of LOCs every time for no significant advantage in either readability or maintainability, while being prone to errors every time your re-implement it.
haylem
the "iterate over array" solution is readable only in scripting languages. if you think about it, it is that readable in C, C++ or Java. you will find swithc...case more readable instead. Also switch...case almost directly translates into machine code (jump instructions) and hence better performance than iterate over lists.
kartheek
+1  A: 

In java:

 List list = Arrays.asList(new Integer[]{1,2});
 Set set = new HashSet(list);
 set.contains(1)
Gadolin
You can do it in one line: if (new HashSet<Integer>(Arrays.asList(1, 2, 3)).contains(2))
mavroprovato
Nice, I wish I was using 1.5>= version with variable number of parameters.
Gadolin
@mavro you can use even less code, skip the HashSet: `if (Arrays.asList(1, 2, 3).contains(2))`
seanizer
+9  A: 

Your approach would indeed seem more natural but that really depends on the language you use for the implementation.

Rationale for the Mess

C being a systems programming language, and fairly close to the hardware (funny though, as we used to consider a "high-level" language, as opposed to writing machine code), it's not exactly expressive.

Modern higher-level languages (again, arguable, lisp is not that modern, historically speaking, but would allow you to do that nicely) allow you to do such things by using built-in constructs or library support (for instances, using Ranges, Tuples or equivalents in languages like Python, Ruby, Groovy, ML-languages, Haskell...).

Possible Solution 1

One option for you would be to implement a function taking an array of values and checking them.

Here's a basic prototype, and I leave the implementation as an exercise to you:

int is_in(int check, int *values, int size); // returns non-zero value if check is in values

However, as you will quickly see, this is very basic and not very flexible:

  • it works only on integers,
  • it works only to compare identical values.

Possible Solution 2

An alternative would be to use pre-processor macros in C (or C++) to achieve a similar behavior, but beware of side effects.

Keep Going

A next step could be to pass a function pointer as an extra parameter to define the behavior at call-point, define several variants and aliases for this, and build yourself a small library of comparators.

The next step then would be to implement a similar thing in C++ using templates to do this on different types with a single implementation.

And then keep going from there to higher-level languages.


Pick the Right Language (or learn to let go!)

Typically, languages favoring functional programming will have built-in support for this sort of thing, for obvious reasons.

Or just learn to accept that some languages can do things that others cannot, and that depending on the job and environment, that's just the way it is.

Maybe a library implements such a thing already, than I am not aware of.

haylem
+1  A: 

A try with only one non-bitwise boolean operator (not advised, not tested):

if( (x&3) ^ x ^ ((x>>1)&1) ^ (x&1) ^ 1 == 0 )

The (x&3) ^ x part should be equal to 0, this ensures that x is between 0 and 3. Other operands will only have the last bit set.

The ((x>>1)&1) ^ (x&1) ^ 1 part ensures last and second to last bits are different. This will apply to 1 and 2, but not 0 and 3.

Benoit
+1. this answer should be accepted. genius.
Sanjay Manohar
+11  A: 

When I started programming it seemed weird to me as well that instead of something like:

(1 < x < 10)

I had to write:

(1 < x && x < 10)

But this is how most programming languages work, and after a while you will get used to it.

So I believe it is perfectly fine to write

( x == 1 || x == 2 )

Writing it this way also has the advantage that other programmers will understand easily what you wrote. Using a function to encapsulate it might just make things more complicated because the other programmers would need to find that function and see what it does.

Only more recent programming languages like Python, Ruby etc. allow you to write it in a simpler, nicer way. That is mostly because these programming languages are designed to increase the programmers productivity, while the older programming languages' main goal was application performance and not so much programmer productivity.

Florin
You can do the first example in Python!
chpwn
+17  A: 

An extension version in C#

step 1: create an extension method

public static class ObjectExtensions
{
    public static bool Either(this object value, params object[] array)
    {
        return array.Any(p => Equals(value, p));
    }
}

step 2: use the extension method

if (x.Either(1,2,3,4,5,6)) 
{
}
else
{
}
Andre Haverdings
For more items it is slightly more efficient to use this:`public static bool IsIn<T>(this T value, params T[] values) where T : IComparable``{`` return values.Length == 0 ? false : new List<T>(values).Contains(value);``}`
Piers Myers
@Piers Myers: Why create a new List<T>?
Joh
@Piers Myers Just use `(Array.IndexOf(values, value) > -1)` so you don't have to create a new list.
Andrew Koester
Wow, I like this a lot! Extension methods are so flippin' awesome!
John Gietzen
@Andrew Koester - yep that's even faster, thanks - it's always good to learn something new.
Piers Myers
+1  A: 

You say the notation (x==1 || x==2) is "awkward and hard to read". I beg to differ. It's different than natural language, but is very clear and easy to understand. You just need to think like a computer.

Also, the notations mentioned in this thread like x in (1,2) are semantically different then what you are really asking, they ask if x is member of the set (1,2), which is not what you are asking. What you are asking is if x equals to 1 or to 2 which is logically (and semantically) equivalent to if x equals to 1 or x equals to 2 which translates to (x==1 || x==2).

Nir Levy
+5  A: 

I doubt I'd ever do this, but to answer your question, here's one way to achieve it in C# involving a little generic type inference and some abuse of operator overloading. You could write code like this:

if (x == Any.Of(1, 2)) {
    Console.WriteLine("In the set.");
}

Where the Any class is defined as:

public static class Any {
    public static Any2<T> Of<T>(T item1, T item2) {
        return new Any2<T>(item1, item2);
    }
    public struct Any2<T> {
        T item1;
        T item2;
        public Any2(T item1, T item2) {
            this.item1 = item1;
            this.item2 = item2;
        }
        public static bool operator ==(T item, Any2<T> set) {
            return item.Equals(set.item1) || item.Equals(set.item2);
        }
        // Defining the operator== requires these three methods to be defined as well:
        public static bool operator !=(T item, Any2<T> set) {
            return !(item == set);
        }
        public override bool Equals(object obj) { throw new NotImplementedException(); }
        public override int GetHashCode() { throw new NotImplementedException(); }
    }
}

You could conceivably have a number of overloads of the Any.Of method to work with 3, 4, or even more arguments. Other operators could be provided as well, and a companion All class could do something very similar but with && in place of ||.

Looking at the disassembly, a fair bit of boxing happens because of the need to call Equals, so this ends up being slower than the obvious (x == 1) || (x == 2) construct. However, if you change all the <T>'s to int and replace the Equals with ==, you get something which appears to inline nicely to be about the same speed as (x == 1) || (x == 2).

Wesley Hill
+3  A: 

In T-SQL

where x in (1,2)
adopilot
+4  A: 

In COBOL (it's been a long time since I've even glanced briefly at COBOL, so I may have a detail or two wrong here):

IF X EQUALS 1 OR 2
...

So the syntax is definitely possible. The question then boils down to "why is it not used more often?"

Well, the thing is, parsing expressions like that is a bit of a bitch. Not when standing alone like that, mind, but more when in compound expressions. The syntax starts to become opaque (from the compiler implementer's perspective) and the semantics downright hairy. IIRC, a lot of COBOL compilers will even warn you if you use syntax like that because of the potential problems.

JUST MY correct OPINION
And, when the syntax starts becoming opaque from the compiler implementor's perspective, it also becomes opaque from the programmer's perspective. Typically, if a reasonable parser can parse something, a human can also.
David Thornley
Humans can parse more complicated expressions before breaking down. Evidence: pretty much all of natural language. :D
JUST MY correct OPINION
A: 

I have a macro that I use a lot that's somewhat close to what you want.

#define ISBETWEEN(Var, Low, High) ((Var) >= (Low) && (Var) <= (High))

ISBETWEEN(x, 1, 2) will return true if x is 1 or 2.

Ricky Bennett
+2  A: 

Pascal has a (limited) notion of sets, so you could do:

if x in [1, 2] then

(haven't touched a Pascal compiler in decades so the syntax may be off)

Ferruccio
It is however limited to ordinal types with <= 256 possible values. The Delphi compiler implements sets as one or more 32 bit bitmasks. In then uses binary ORs to check for membership.
Gerry
A: 

Neither C, C++, VB.net, C#.net, nor any other such language I know of has an efficient way to test for something being one of several choices. Although (x==1 || x==2) is often the most natural way to code such a construct, that approach sometimes requires the creation of an extra temporary variable:

  tempvar = somefunction(); // tempvar only needed for 'if' test:
  if (tempvar == 1 || tempvar == 2)
    ...

Certainly an optimizer should be able to effectively get rid of the temporary variable (shove it in a register for the brief time it's used) but I still think that code is ugly. Further, on some embedded processors, the most compact and possibly fastest way to write (x == const1 || x==const2 || x==const3) is:

  movf  _x,w             ; Load variable X into accumulator
  xorlw const1           ; XOR with const1
  btfss STATUS,ZERO      ; Skip next instruction if zero
   xorlw const1 ^ const2 ; XOR with (const1 ^ const2)
  btfss STATUS,ZERO      ; Skip next instruction if zero
   xorlw const2 ^ const3 ; XOR with (const2 ^ const3)
  btfss STATUS,ZERO      ; Skip next instruction if zero
   goto NOPE

That approach require two more instructions for each constant; all instructions will execute. Early-exit tests will save time if the branch is taken, and waste time otherwise. Coding using a literal interpretation of the separate comparisons would require four instructions for each constant.

If a language had an "if variable is one of several constants" construct, I would expect a compiler to use the above code pattern. Too bad no such construct exists in common languages.

(note: Pascal does have such a construct, but run-time implementations are often very wasteful of both time and code space).

supercat