Useful alternative control structures?

后端 未结 28 915
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

相关标签:
28条回答
  • 2021-01-30 02:53

    I'd like to see a keyword for grouping output. Instead of this:

            int lastValue = 0;
    
            foreach (var val in dataSource)
            {
                if (lastValue != val.CustomerID)
                {                    
                    WriteFooter(lastValue);
                    WriteHeader(val);
                    lastValue = val.CustomerID;
                }
                WriteRow(val);
            }
            if (lastValue != 0)
            {
                WriteFooter(lastValue);
            }
    

    how about something like this:

            foreach(var val in dataSource)
            groupon(val.CustomerID)
            {            
                startgroup
                {
                    WriteHeader(val);
                }
                endgroup
                {
                    WriteFooter(val)
                }
            }
            each
            {
                WriteRow(val);
            }
    

    If you have a decent platform, controls, and/or reporting formatting you won't need to write this code. But it's amazing how often I find myself doing this. The most annoying part is the footer after the last iteration - it's hard to do this in a real life example without duplicating code.

    0 讨论(0)
  • 2021-01-30 02:53

    One of the control structures that isn't available in many languages is the case-in type structure. Similar to a switch type structure, it allows you to have a neatly formatted list of possible options, but matches the first one that's true (rather then the first one that matches the input). A LISP of such such (which does have it):

    (cond
       ((evenp a) a)        ;if a is even return a
       ((> a 7) (/ a 2))    ;else if a is bigger than 7 return a/2
       ((< a 5) (- a 1))    ;else if a is smaller than 5 return a-1
       (t 17))              ;else return 17
    

    Or, for those that would prefer a more C-like format

    cond 
        (a % 2 == 0): 
            a;     break;
        (a > 7):
            a / 2; break;
        (a < 5):
            a - 1; break;
        default:
            17;    break;
    

    It's basically a more accurate representation of the if/elseif/elseif/else construct than a switch is, and it can come in extremely handing in expressing that logic in a clean, readable way.

    0 讨论(0)
  • 2021-01-30 02:57

    One that's fairly common is the infinite loop. I'd like to write it like this:

    forever {
      // ...
    }
    
    0 讨论(0)
  • 2021-01-30 03:00

    With (lisp-style) macros, tail-calls, and continuations all of this is quaint.

    With macros, if the standard control flow constructs are not sufficient for a given application, the programmer can write their own (and so much more). It would only require a simple macro to implement the constructs you gave as an example.

    With tail-calls, one can factor out complex control flow patters (such as implementing a state machine) into functions.

    Continuations are a powerful control flow primitive (try/catch are a restricted version of them). Combined with tail-calls and macros, complex control flow patterns (backtracking, parsing, etc.) become straight-forward. In addition, they are useful in web programming as with them you can invert the inversion of control; you can have a function that asks the user for some input, do some processing, asks the user for more input, etc.

    To paraphrase the Scheme standard, instead of piling more features onto your language, you should seek to remove the limitations that make the other features appear necessary.

    0 讨论(0)
  • 2021-01-30 03:01

    This is just a general idea and syntax:

    if (cond)
       //do something
    else (cond)
       //do something
    also (cond)
       //do something
    else
       //do something
    end
    

    ALSO condition is always evaluated. ELSE works as usual.

    It works for case too. Probably it is a good way to eliminate break statement:

    case (exp)
       also (const)
          //do something
       else (const)
          //do something
       also (const)
          //do something
       else
          //do something
    end
    

    can be read as:

    switch (exp)
       case (const)
          //do something
       case (const)
          //do something
          break
       case (const)
          //do something
       default
          //do something
    end
    

    I don't know if this is useful or simple to read but it's an example.

    0 讨论(0)
  • 2021-01-30 03:01

    Sometimes, I need to have a foreach loop with an index. It could be written like this:

    foreach (index i) (var item in list) {
      // ...
    }
    

    (I'm not particularly fond of this syntax, but you get the idea)

    0 讨论(0)
提交回复
热议问题