Useful alternative control structures?

后端 未结 28 917
礼貌的吻别
礼貌的吻别 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 03:05

    I propose the "then" operator. It returns the left operand on the first iteration and the right operand on all other iterations:

    var result = "";
    foreach (var item in items) {
        result += "" then ", ";
        result += item;
    }
    

    in the first iteration it adds "" to the result in all others it adds ", ", so you get a string that contains each item separated by commas.

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

    Most languages have built-in functions to cover the common cases, but "fencepost" loops are always a chore: loops where you want to do something on each iteration and also do something else between iterations. For example, joining strings with a separator:

    string result = "";
    for (int i = 0; i < items.Count; i++) {
        result += items[i];
        if (i < items.Count - 1) result += ", "; // This is gross.
        // What if I can't access items by index?
        // I have off-by-one errors *every* time I do this.
    }
    

    I know folds can cover this case, but sometimes you want something imperative. It would be cool if you could do:

    string result = "";
    foreach (var item in items) {
        result += item;
    } between {
        result += ", ";
    }
    
    0 讨论(0)
  • 2021-01-30 03:07

    Generators, in Python, are genuinely novel if you've mostly worked with non-functional languages. More generally: continuations, co-routines, lazy lists.

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

    This probably doesn't count, but in Python, I was upset there was no do loop.

    Anto ensure I get no upvotes for this answer, I wind up annoyed at any language I work in for any period of time that lacks goto's.

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

    How about PL/I style "for" loop ranges? The VB equivalent would be:

    ' Counts 1, 2, ... 49, 50, 23, 999, 998, ..., 991, 990
      For I = 1 to 50, 23, 999 to 990 Step -1
    

    The most common usage I can see would be to have a loop run for a list of indices, and then throw in one more. BTW, a For-Each usage could also be handy:

    ' Bar1, Bar2, Bar3 are an IEnum(Wazoo); Boz is a Wazoo
      For Each Foo as Wazoo in Bar1, Bar2, Enumerable.One(Boz), Bar3
    

    This would run the loop on all items in Bar1, all items in Bar2, Boz, and Bar3. Linq would probably allow this without too much difficulty, but intrinsic language support might be a little more efficient.

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

    Labeled loops are something I find myself missing sometimes from mainstream languages. e.g.,

    int i, j;
    for outer ( i = 0; i < M; ++i )
        for ( j = 0; j < N; ++j )
            if ( l1[ i ] == l2[ j ] )
               break outer;
    

    Yes, I can usually simulate this with a goto, but an equivalent for continue would require you to move the increment to the end of loop body after the label, hurting the readability. You can also do this by setting a flag in the inner loop and checking it at each iteration of the outer loop, but it always looks clumsy.

    (Bonus: I'd sometimes like to have a redo to go along with continue and break. It would return to the start of the loop without evaluating the increment.)

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