Loop iterator - example.txt

Kyle Furlong kylefurlong at gmail.com
Sat Apr 29 23:13:16 PDT 2006


Unknown W. Brackets wrote:
> I would probably do something more like (assuming there is no other code 
> but the tests):
> 
> int i = 0;
> while (i < 10)
> {
>     if (string[i] == something)
>     {
>         i += some_other_function();
>         continue;
>     }
>     else if (string[i] == something_else)
>     {
>         i += some_other_function2();
>         continue;
>     }
> 
>     // Otherwise, simply execute the "i++" and re-test
>     i++;
> }
> 
> I mean, while is a keyword too, isn't it?  I really am unclear on how 
> it's any more obtuse... perhaps you're just not as used to it?
> 
> Do you cut everything, even cheese and fruit, with a steak knife? 
> Perhaps you do.  But do master chefs as well?  Or do they use the right 
> knives for the right purposes?
> 
> Actually, I might do this depending on the number of tests:
> 
> int pos = 0;
> while (pos < string.length)
> {
>     switch (string[pos])
>     {
>     case something:
>         pos += some_other_function(string[pos .. string.length]);
>         break;
> 
>     case something_else:
>         pos += some_other_function2(string[pos .. string.length]);
>         break;
> 
>     default:
>         pos++;
>     }
> }
> 
> Which, to me, seems much more clear than any "retry" would.  In fact, 
> "retry" would seem incredibly unclear to me.  That's just me.  I don't 
> see the concept of "retrying" anywhere.
> 
> The above code looks somewhat similar to code I've used in an xml 
> document parser and a simple abbreviated xpath expression evaluator.  I 
> can't even remember the last time I've used/wanted anything like a "retry".
> 
> Clearly, just my opinion.
> 
> -[Unknown]
> 
> 
>> Here's an idea:
>>
>> There should be a way in D to allow the reconsideration of a for..loop 
>> test
>> clause without executing the increment clause.
>>
>> Using the terminology:
>> for (initialize-clause; conditional-clause; increment-clause)
>>
>> Example:
>> int i;
>> for (i=0; i<10; i++)
>> {
>> if (string.substr(i,1) == something)
>> {
>> i += some_other_function();
>> retry;
>> }
>> else if (string.substr(i,1) == something_else)
>> {
>> i += some_other_function2();
>> retry;
>> }
>> // Otherwise, simply execute the "i++" and re-test
>> }
>>
>> I propose the name "retry" for the "retest without increment-clause" 
>> command, to
>> be used in a manner similar syntax-wise to the way "break" is used today.
>> "Retry" would simply bypass the increment-clause and proceed straight 
>> to the
>> conditional-clause code section, thereby allowing subsequent passes 
>> through the
>> for loop without the requisite and occasionally unnecessary 
>> auto-incrementation.
>>
>> It would just be a way to give for loops a little more natural utility 
>> without
>> having to do some rather obtuse programming techniques, such as using 
>> goto's or
>> enclosing the code in a while or do loop, etc.
>>
>> - Rick C. Hodgin
>>
>>
>>
>> int i;
>> for (i=0; i<10; i++)
>> {
>>     if (string.substr(i,1) == something)
>>     {
>>         i += some_other_function();
>>         retry;
>>     }
>>     else if (string.substr(i,1) == something_else)
>>     {
>>         i += some_other_function2();
>>         retry;
>>     }
>>     // Otherwise, simply execute the "i++" and re-test
>> }

This, to me, is the right solution to the design pattern. No need for a 
new keyword, and the functionality is clearly the same.



More information about the Digitalmars-d mailing list