"or" operator without repeating the left condition again

Since I started programming correctly using the good old one VB6and until today, I often still burn (and just have) this when programming:

if x == something or x == somethingelse

I often end up writing:

if x == something or somethingelse

Just out of sheer interest, does any language / languages ​​support there?

+5
source share
8 answers

Python does something like:

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

injust checks if an item exists in the given list. Similarly, in Haskell:

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

I believe that this can be done in most languages ​​that allow you to express a list type.

+11
source

SQL in: x in (something, somethingelse), , - .

#, , : if (x.In("something", "somethingelse"))...

#:

public static bool In<T>(this T item, params T[] test_values) {
  foreach (T test_value in test_values)
  {
    if (test_value.Equals(item))
    {
      return true;
    }
  }

  return false;
}
+6

- :

myArray[something, somethingElse].Contains(x)

... , .

+3

Icon programming language . , SNOBOL, . , . ,

if x == (something | somethingelse) then write("Goodie!")

:

  • something
  • something x
  • , , .
  • , ! somethingelse.
  • , . if .

: , , , . ,

lo <= x < limit

:

(lo <= x) < limit

, lo , x, , . ( , - .) lo x, lo <= x x. , , x < limit, , .

, . , , , Unix, Icon . , .

R.I.P.

+2

"switch" :

switch (x)
{
    case 1:
    case 2:
    {
      // do whatever
      break;
    }

    default:
    {
        // else case
        break;       
    }
}

, C ++, , .

+1

MATLAB , :

if any(x == [something somethingelse]) ...
%# OR
if ismember(x,[something somethingelse]) ...
+1

Perl: $foo ~~ ['string',$number,qr/regex/]

0

A in [x, y, z]

,

in(A, x, y, z)

, , , cmp (A, x, y, z)

A in x y z

if (A == x or y or z).

if (A == x and y and z).

varargs, , c, ++, # java5.

java .

boolean or(String lhs, String... rhs){
  for(String z: rhs){
    if (lhs.equals(z) return true;
  }
  return false;
}

boolean and(String lhs, String... rhs){
  for(String z: rhs){
    if (!lhs.equals(z) return false;
  }
  return true;
}

Varargs , ,

or (A, x)
or (A, x, y)
or (A, x, y, z)

, arg. Java 5 .

<T extends Comparable<T>>boolean or(T lhs, T... rhs){
  for(T z: rhs){
    if (lhs.compareTo(z)==0) return true;
  }
  return false;
}

<T extends Comparable<T>>boolean and(T lhs, T... rhs){
  for(T z: rhs){
    if (lhs.compareTo(z)!=0) return false;
  }
  return true;
}

, , .

and(stringA, stringx, stringy)
or(dateA, datex)

, Java ,

stringA && stringx, stringy
dateA || datex, datey, datez

++ varargs, , .

: , ,

public class <T extends Comparable<T>> Comparigator{
  public Comparigator(T lhs){
    this.lhs = lhs;
  }
  final private T lhs;

  static public <T extends Comparable<T>> Comparigator is(T lhs){
    return (T)new Comparigator(lhs);
  }

  public boolean inAny(T... rhs){
    for(T z: rhs){
      if (this.lhs.compareTo(z)==0) return true;
    }
    return false;
  }

  public boolean inAll(T... rhs){
    for(T z: rhs){
      if (this.lhs.compareTo(z)!=0) return false;
    }
    return true;
  }

  public boolean gtAny(T... rhs){
    for(T z: rhs){
      if (this.lhs.compareTo(z)>0) return true;
    }
    return false;
  }

  public boolean gtAll(T... rhs){
    for(T z: rhs){
      if (this.lhs.compareTo(z)<=0) return false;
    }
    return true;
  }
}

,

import Comparigator;
.....

is(A).inAny(x,y,z); // or
is(A).inAll(w,x,y,z); // and

is(B).gtAny(k,l,m);
is(C).gtAll(j,k);

, , , gtany, gtall, ltany, ltall .., .

0

All Articles