View mode: basic / threaded / horizontal-split · Log in · Help
April 28, 2006
Loop iterator - example.txt
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
April 28, 2006
Re: Loop iterator - example.txt
On Sat, 29 Apr 2006 06:31:07 +1000, Rick C. Hodgin  
<Rick_member@pathlink.com> wrote:

> 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.
>

...

>
> 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.

Yes! I use this construct daily with the Progress 4GL our company employs.  
It elegantly implements a common idiom without a whole lot of twisting and  
obscurity.

-- 
Derek Parnell
Melbourne, Australia
April 29, 2006
Re: Loop iterator - example.txt
"Rick C. Hodgin" <Rick_member@pathlink.com> wrote in message
news:e2tu2b$kv8$1@digitaldaemon.com...
> 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.

Very good idea.  I find myself decrementing the iterator in order to achieve
the same. This approach doesn't work in more complex cases and I've recently
run into a complex situation where "retry" would have saved me a lot of
headaches.

-Craig
April 29, 2006
Re: Loop iterator - example.txt
Rick C. Hodgin wrote:
> 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
> }

Very neat, got my vote!

Regards,
Alexander Panek
April 29, 2006
Re: Loop iterator - example.txt
Rick C. Hodgin wrote:
> 
> 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.

I like it.  It would be particularly nice if this were made to work with 
foreach as well, since there's currently no way to avoid progress for 
each iteration.


Sean
April 29, 2006
Re: Loop iterator - example.txt
Rick C. Hodgin wrote:
> 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
> }

For the case where you don't have any continues, you can do the code 
this way instead:

  for (i=0; i<10; )
  {
    if (string.substr(i,1) == something)
    {
      i += some_other_function();
      continue;
    }
    else if (string.substr(i,1) == something_else)
    {
      i += some_other_function2();
      continue;
    }
    // ############ Execute the increment expression here: #######
    i++;
  }

The remaining case, where you want to use continues and retries in the 
same for, well, I don't think it's a common enough case that makes it 
worth the introduction of a new keyword just some trivial syntactic sugar.
In fact, the very idea seems like a very awkward idiom to me. I would 
like to examine a real example, can someone post one?



-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
April 29, 2006
Re: Loop iterator - example.txt
Rick C. Hodgin wrote:
> 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 know goto's are evil, but I tend to write such as:

int i;
for (i=0; i<10; i++)
{
  Lretry:
    if (string.substr(i,1) == something)
    {
        i += some_other_function();
        goto Lretry;
    }
    else if (string.substr(i,1) == something_else)
    {
        i += some_other_function2();
        goto Lretry;
    }
    // Otherwise, simply execute the "i++" and re-test
}
April 30, 2006
Re: Loop iterator - example.txt
On Sun, 30 Apr 2006 06:02:10 +1000, Bruno Medeiros  
<brunodomedeirosATgmail@SPAM.com> wrote:


> In fact, the very idea seems like a very awkward idiom to me. I would  
> like to examine a real example, can someone post one?

We use it in the area of retrying a database transaction after some sort  
of exception condition has happened. It has nothing directly to do with  
loop index maintenance.

In pseudo-code ...

   foreach (inout Customer cust; CustomerSet )
   {
       try {
       cust.name = UI.CustName;
       cust.address = UI.Address;
       . . .
       }
       catch (BadUI e)
       {
           // Recover from the (rare) UI data error
           . . .
           retry; // Reprocess the same customer record.
       }
   }

-- 
Derek Parnell
Melbourne, Australia
April 30, 2006
Re: Loop iterator - example.txt
Derek Parnell wrote:
> On Sun, 30 Apr 2006 06:02:10 +1000, Bruno Medeiros  
> <brunodomedeirosATgmail@SPAM.com> wrote:
> 
> 
>> In fact, the very idea seems like a very awkward idiom to me. I would  
>> like to examine a real example, can someone post one?
> 
> 
> We use it in the area of retrying a database transaction after some 
> sort  of exception condition has happened. It has nothing directly to do 
> with  loop index maintenance.
> 
> In pseudo-code ...
> 
>    foreach (inout Customer cust; CustomerSet )
>    {
>        try {
>        cust.name = UI.CustName;
>        cust.address = UI.Address;
>        . . .
>        }
>        catch (BadUI e)
>        {
>            // Recover from the (rare) UI data error
>            . . .
>            retry; // Reprocess the same customer record.
>        }
>    }
> 

Does that retry that instance, or retry the entire loop? Couldn't the 
semantics be either, given the appropriate condition?
April 30, 2006
Re: Loop iterator - example.txt
On Sun, 30 Apr 2006 10:28:13 +1000, kris <foo@bar.com> wrote:

> Derek Parnell wrote:
>> On Sun, 30 Apr 2006 06:02:10 +1000, Bruno Medeiros   
>> <brunodomedeirosATgmail@SPAM.com> wrote:
>>
>>> In fact, the very idea seems like a very awkward idiom to me. I would   
>>> like to examine a real example, can someone post one?
>>   We use it in the area of retrying a database transaction after some  
>> sort  of exception condition has happened. It has nothing directly to  
>> do with  loop index maintenance.
>>  In pseudo-code ...
>>     foreach (inout Customer cust; CustomerSet )
>>    {
>>        try {
>>        cust.name = UI.CustName;
>>        cust.address = UI.Address;
>>        . . .
>>        }
>>        catch (BadUI e)
>>        {
>>            // Recover from the (rare) UI data error
>>            . . .
>>            retry; // Reprocess the same customer record.
>>        }
>>    }
>>
>
> Does that retry that instance, or retry the entire loop? Couldn't the  
> semantics be either, given the appropriate condition?

Just the instance and not the entire loop. There is a 'restart' key word  
to do the whole loop thing.


-- 
Derek Parnell
Melbourne, Australia
« First   ‹ Prev
1 2 3 4 5
Top | Discussion index | About this forum | D home