Useful alternative control structures?

后端 未结 28 914
礼貌的吻别
礼貌的吻别 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:48

    This is similar to the response by @Paul Keister.

    (mumble, mumble) years ago, the application I was working on had lots of variations of so-called control-break processing -- all that logic that goes into breaking sorted rows of data into groups and subgroups with headers and footers. As the application was written in LISP, we had captured the common idioms in a macro called WITH-CONTROL-BREAKS. If I were to transpose that syntax into the ever-popular squiggly form, it might look something like this:

    withControlBreaks (x, y, z : readSortedRecords()) {
      first (x) :     { emitHeader(x); subcount = 0; }
      first (x, y) :  { emitSubheader(x, y); zTotal = 0; }
      all (x, y, z) : { emitDetail(x, y, z); ztotal += z; }
      last (x, y) :   { emitSubfooter(x, y, zTotal); ++subCount; }
      last (x) :      { emitFooter(x, subcount); }
    }
    

    In this modern era, with widespread SQL, XQuery, LINQ and so on, this need does not seem to arise as much as it used to. But from time to time, I wish that I had that control structure at hand.

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

    ignoring - To ignore exceptions occuring in a certain block of code.

    try {
      foo()
    } catch {
      case ex: SomeException => /* ignore */
      case ex: SomeOtherException => /* ignore */
    }
    

    With an ignoring control construct, you could write it more concisely and more readably as:

    ignoring(classOf[SomeException], classOf[SomeOtherException]) {
      foo()
    }
    

    [ Scala provides this (and many other Exception handling control constructs) in its standard library, in util.control package. ]

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

    This is a bit of a joke, but you can get the behavior you want like this:

    #include <iostream>
    #include <cstdlib>
    
    int main (int argc, char *argv[])
    {
      int N = std::strtol(argv[1], 0, 10); // Danger!
      int state = 0;
      switch (state%2) // Similar to Duff's device.
      {
        do {
          case 1: std::cout << (2*state) << " B" << std::endl;
          case 0: std::cout << (2*state+1) << " A" << std::endl; ++state;
        } while (state <= N);
          default: break;
      }
    
      return 0;
    }
    

    p.s. formatting this was a bit difficult and I'm definitely not happy with it; however, emacs does even worse. Anyone care to try vim?

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

    How about iterating with a moving window (of n elements instead of 1) through a list? This is tangentially related @munificent's answer, I think.

    Something like

    #python
    #sum of adjacent elements
    for x,y in pairs(list):
        print x + y
    
    def pairs(l):              
        i=0                    
        while i < len(l)-1:    
            yield (l[i],l[i+1])
            i+=1               
    

    It is useful for certain types of things. Don't get me wrong, this is easy to implement as a function, but I think a lot of people try to bring out for and while loops when there are more specific/descriptive tools for the job.

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

    D's scope guards are a useful control structure that isn't seen very often.

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

    How about

    alternate {
        statement 1,
        statement 2,
        [statement 3,...]
    }
    

    for cycling through the available statements on each successive pass.

    Edit: trivial examples

    table_row_color = alternate(RED, GREEN, BLUE);
    
    player_color = alternate(color_list); // cycles through list items
    
    alternate(
        led_on(),
        led_off()
    );
    

    Edit 2: In the third example above the syntax is maybe a bit confusing as it looks like a function. In fact, only one statement is evaluated on each pass, not both. A better syntax might be something like

    alternate {
        led_on();
    }
    then {
        led_off();
    }
    

    Or something to that effect. However I do like the idea that the result of which ever is called can be used if desired (as in the color examples).

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