Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Any language supports this type of syntax?

Any language supports this type of syntax?

Scheduled Pinned Locked Moved The Lounge
question
20 Posts 12 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Eytukan

    int nThatVeryLongVariableYourMateNamed = 0;

    if(nThatVeryLongVariableYourMateNamed != (0,1,2))
    {
    //Do something
    }

    Instead of having to do like:

    int nThatVeryLongVariableYourMateNamed = 0;

    if(nThatVeryLongVariableYourMateNamed != 0 &&
    nThatVeryLongVariableYourMateNamed != 1 &&
    nThatVeryLongVariableYourMateNamed != 2
    )
    {
    //Do something
    }

    -VUNIC

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #4

    As Pete said, trivial to implement in C#:

    public static class SomeExtensions
    {
    public static bool IsOneOf(this T value, params T[] options)
    {
    if (options is null || options.Length == 0) return false;
    return Array.IndexOf(options, value) != -1;
    }

    public static bool IsNotOneOf(this T value, params T\[\] options)
    {
        if (options is null || options.Length == 0) return true;
        return Array.IndexOf(options, value) == -1;
    }
    

    }

    ...

    if (nThatVeryLongVariableYourMateNamed.IsNotOneOf(0, 1, 2))


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    D 1 Reply Last reply
    0
    • E Eytukan

      int nThatVeryLongVariableYourMateNamed = 0;

      if(nThatVeryLongVariableYourMateNamed != (0,1,2))
      {
      //Do something
      }

      Instead of having to do like:

      int nThatVeryLongVariableYourMateNamed = 0;

      if(nThatVeryLongVariableYourMateNamed != 0 &&
      nThatVeryLongVariableYourMateNamed != 1 &&
      nThatVeryLongVariableYourMateNamed != 2
      )
      {
      //Do something
      }

      -VUNIC

      Q Offline
      Q Offline
      QuantumPlumber
      wrote on last edited by
      #5

      Fortran computed GOTO comes pretty close . . . 11 CONTROL STATEMENTS[^] There - that'll flush out the zealots.

      Treading on the toes of giants . . .

      1 Reply Last reply
      0
      • P Pete OHanlon

        You could do that as an extension method in C# pretty easily. The syntax would be something like ```csharp if (nThatVeryLongVariableYourMateNamed.Excludes(0,1,2)) ```

        This space for rent

        E Offline
        E Offline
        Eric Lynch
        wrote on last edited by
        #6

        Not nearly as performant as the original. Though, if avoiding repetition is the OPs goal, a couple of other C# possibilities include: :)

        int n = nThatVeryLongVariableYourMateNamed;
        if (n == 0 || n == 1 || n == 2)
        {
        }

        switch(nThatVeryLongVariableYourMateNamed)
        {
        case 0:
        case 1
        case 2
        break;
        }

        private static readonly HashSet validValues = new HashSet { 0, 1, 2};
        .
        .
        .
        if (validValues.Contains(nThatVeryLongVariableYourMateNamed))
        {
        }

        if (new int[] { 0, 1, 2}.Contains(nThatVeryLongVariableYourMateNamed))
        {
        }

        In answer to the original question, I think there are some languages that support syntactic sugar for creating/checking sets. Though, none I commonly use. If I recall, in my hazy memory, I think the largely dead PASCAL language used to have such support :) In general, it's not much of a problem. Almost all languages, have a decently concise syntax for creating/checking sets. They simply do away with the fluff of formalizing it as a separate concept in the language.

        G 1 Reply Last reply
        0
        • E Eytukan

          int nThatVeryLongVariableYourMateNamed = 0;

          if(nThatVeryLongVariableYourMateNamed != (0,1,2))
          {
          //Do something
          }

          Instead of having to do like:

          int nThatVeryLongVariableYourMateNamed = 0;

          if(nThatVeryLongVariableYourMateNamed != 0 &&
          nThatVeryLongVariableYourMateNamed != 1 &&
          nThatVeryLongVariableYourMateNamed != 2
          )
          {
          //Do something
          }

          -VUNIC

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          Put the values in a Hashset.

          1 Reply Last reply
          0
          • E Eric Lynch

            Not nearly as performant as the original. Though, if avoiding repetition is the OPs goal, a couple of other C# possibilities include: :)

            int n = nThatVeryLongVariableYourMateNamed;
            if (n == 0 || n == 1 || n == 2)
            {
            }

            switch(nThatVeryLongVariableYourMateNamed)
            {
            case 0:
            case 1
            case 2
            break;
            }

            private static readonly HashSet validValues = new HashSet { 0, 1, 2};
            .
            .
            .
            if (validValues.Contains(nThatVeryLongVariableYourMateNamed))
            {
            }

            if (new int[] { 0, 1, 2}.Contains(nThatVeryLongVariableYourMateNamed))
            {
            }

            In answer to the original question, I think there are some languages that support syntactic sugar for creating/checking sets. Though, none I commonly use. If I recall, in my hazy memory, I think the largely dead PASCAL language used to have such support :) In general, it's not much of a problem. Almost all languages, have a decently concise syntax for creating/checking sets. They simply do away with the fluff of formalizing it as a separate concept in the language.

            G Offline
            G Offline
            G3Coder
            wrote on last edited by
            #8

            Yes - Delphi does:

            Result: Integer;
            ...
            if aPrompt and not (Result in [0, 1, 2, 4]) then
            ...

            Not quite dead BTW - that's production code right there -> but its getting rarer these days. G

            1 Reply Last reply
            0
            • E Eytukan

              int nThatVeryLongVariableYourMateNamed = 0;

              if(nThatVeryLongVariableYourMateNamed != (0,1,2))
              {
              //Do something
              }

              Instead of having to do like:

              int nThatVeryLongVariableYourMateNamed = 0;

              if(nThatVeryLongVariableYourMateNamed != 0 &&
              nThatVeryLongVariableYourMateNamed != 1 &&
              nThatVeryLongVariableYourMateNamed != 2
              )
              {
              //Do something
              }

              -VUNIC

              D Offline
              D Offline
              David ONeil
              wrote on last edited by
              #9

              C++ equivalent:

              bool contains(int var, std::vector ints) {
              for (int x :ints) if (var == x) return true;
              return false;
              }

              ...

              int veryLongVarName = 12;
              if (contains(veryLongVarName, { 1, 2, 3 })) {
              OutputDebugString(L"Matches! Now we have fire!");
              }

              The forgotten roots of science | C++ Programming | DWinLib

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                As Pete said, trivial to implement in C#:

                public static class SomeExtensions
                {
                public static bool IsOneOf(this T value, params T[] options)
                {
                if (options is null || options.Length == 0) return false;
                return Array.IndexOf(options, value) != -1;
                }

                public static bool IsNotOneOf(this T value, params T\[\] options)
                {
                    if (options is null || options.Length == 0) return true;
                    return Array.IndexOf(options, value) == -1;
                }
                

                }

                ...

                if (nThatVeryLongVariableYourMateNamed.IsNotOneOf(0, 1, 2))


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                D Offline
                D Offline
                Dominic Burford
                wrote on last edited by
                #10

                Now that's nice. I'm going to have to borrow that :-D

                "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare Home | LinkedIn | Google+ | Twitter

                1 Reply Last reply
                0
                • E Eytukan

                  int nThatVeryLongVariableYourMateNamed = 0;

                  if(nThatVeryLongVariableYourMateNamed != (0,1,2))
                  {
                  //Do something
                  }

                  Instead of having to do like:

                  int nThatVeryLongVariableYourMateNamed = 0;

                  if(nThatVeryLongVariableYourMateNamed != 0 &&
                  nThatVeryLongVariableYourMateNamed != 1 &&
                  nThatVeryLongVariableYourMateNamed != 2
                  )
                  {
                  //Do something
                  }

                  -VUNIC

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  {
                  int localVLVYMN = thatVeryLongVariableYourMateNamed;
                  }

                  As a single block, so that your temp-int is limited in scope. Also spitting on the hungarian notation - I would not tough that code with a polearm :thumbsup:

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                  Richard DeemingR 1 Reply Last reply
                  0
                  • L Lost User

                    {
                    int localVLVYMN = thatVeryLongVariableYourMateNamed;
                    }

                    As a single block, so that your temp-int is limited in scope. Also spitting on the hungarian notation - I would not tough that code with a polearm :thumbsup:

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #12

                    For extra C#7 goodness:

                    {
                    ref int localVLVYMN = ref thatVeryLongVariableYourMateNamed;
                    ...
                    }

                    Now any changes to your alias will be reflected in the original. :) An added benefit for large structs: you avoid making a copy of the value. Ref return values and ref locals (C# Guide) | Microsoft Docs[^]


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    L 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      For extra C#7 goodness:

                      {
                      ref int localVLVYMN = ref thatVeryLongVariableYourMateNamed;
                      ...
                      }

                      Now any changes to your alias will be reflected in the original. :) An added benefit for large structs: you avoid making a copy of the value. Ref return values and ref locals (C# Guide) | Microsoft Docs[^]


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      Richard Deeming wrote:

                      Now any changes to your alias will be reflected in the original. :)

                      More ways to obfuscate my code :-D

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                      Richard DeemingR 1 Reply Last reply
                      0
                      • L Lost User

                        Richard Deeming wrote:

                        Now any changes to your alias will be reflected in the original. :)

                        More ways to obfuscate my code :-D

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #14

                        More job security. :-D


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        L 1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          More job security. :-D


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #15

                          Can't argue with that :)

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                          1 Reply Last reply
                          0
                          • E Eytukan

                            int nThatVeryLongVariableYourMateNamed = 0;

                            if(nThatVeryLongVariableYourMateNamed != (0,1,2))
                            {
                            //Do something
                            }

                            Instead of having to do like:

                            int nThatVeryLongVariableYourMateNamed = 0;

                            if(nThatVeryLongVariableYourMateNamed != 0 &&
                            nThatVeryLongVariableYourMateNamed != 1 &&
                            nThatVeryLongVariableYourMateNamed != 2
                            )
                            {
                            //Do something
                            }

                            -VUNIC

                            J Offline
                            J Offline
                            Joan M
                            wrote on last edited by
                            #16

                            Wasn't the old visual basic capable to use WITH?

                            www.robotecnik.com[^] - robots, CNC and PLC programming

                            E 1 Reply Last reply
                            0
                            • J Joan M

                              Wasn't the old visual basic capable to use WITH?

                              www.robotecnik.com[^] - robots, CNC and PLC programming

                              E Offline
                              E Offline
                              Eytukan
                              wrote on last edited by
                              #17

                              You remember the syntax? :)

                              Full Reset

                              1 Reply Last reply
                              0
                              • E Eytukan

                                int nThatVeryLongVariableYourMateNamed = 0;

                                if(nThatVeryLongVariableYourMateNamed != (0,1,2))
                                {
                                //Do something
                                }

                                Instead of having to do like:

                                int nThatVeryLongVariableYourMateNamed = 0;

                                if(nThatVeryLongVariableYourMateNamed != 0 &&
                                nThatVeryLongVariableYourMateNamed != 1 &&
                                nThatVeryLongVariableYourMateNamed != 2
                                )
                                {
                                //Do something
                                }

                                -VUNIC

                                D Offline
                                D Offline
                                DaveAuld
                                wrote on last edited by
                                #18

                                In Python you can use range(). So, for [0,1,2] you would do either;

                                if nThatVeryLongVariableYourMateNamed not in [0,1,2]:

                                or

                                if nThatVeryLongVariableYourMateNamed not in range(3):

                                or

                                if nThatVeryLongVariableYourMateNamed not in range(0,3):

                                or

                                if nThatVeryLongVariableYourMateNamed not in range(0,3,1):

                                Dave Find Me On:Web|Youtube|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject

                                E 1 Reply Last reply
                                0
                                • D DaveAuld

                                  In Python you can use range(). So, for [0,1,2] you would do either;

                                  if nThatVeryLongVariableYourMateNamed not in [0,1,2]:

                                  or

                                  if nThatVeryLongVariableYourMateNamed not in range(3):

                                  or

                                  if nThatVeryLongVariableYourMateNamed not in range(0,3):

                                  or

                                  if nThatVeryLongVariableYourMateNamed not in range(0,3,1):

                                  Dave Find Me On:Web|Youtube|Facebook|Twitter|LinkedIn Folding Stats: Team CodeProject

                                  E Offline
                                  E Offline
                                  Eytukan
                                  wrote on last edited by
                                  #19

                                  Cool :thumbsup:

                                  Full Reset

                                  1 Reply Last reply
                                  0
                                  • P Pete OHanlon

                                    You could do that as an extension method in C# pretty easily. The syntax would be something like ```csharp if (nThatVeryLongVariableYourMateNamed.Excludes(0,1,2)) ```

                                    This space for rent

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #20

                                    Copy paste from SOverflow

                                    static class Extensions
                                    {

                                    public static bool In(this T item, params T\[\] items)
                                    {
                                        if (items == null)
                                            throw new ArgumentNullException("items");
                                    
                                        return items.Contains(item);
                                    }
                                    

                                    }

                                    class Program
                                    {

                                    static void Main()
                                    {
                                    
                                    
                                        int myValue = 1;
                                    
                                        if (myValue.In(1, 2, 3))
                                            // Do Somthing...
                                    
                                        string ds = "Bob";
                                    
                                        if (ds.In("andy", "joel", "matt")) 
                                        // Do Someting...
                                    }
                                    

                                    }

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups