Empowering foreach with in-place iteration

BCS ao at pathlink.com
Sun Sep 9 09:12:52 PDT 2007


Reply to Janice,

> There are some things that iterators can do that foreach can't. I
> /don't/ want to add iterators to D. Instead, I'd like to see foreach
> enhanced, so that it can do the things it currently can't.
> 
> Here's my second example.
> 
> string unescape(string s)
> {
>   string r;
>   foreach (char c; s)
>   {
>     if c == '\')
>     {
>        iterate(s); /* advances one step through s; modifies c */
>        r ~= f(c);
>     }
>     else
>     {
>       r ~= c;
>     }
>   }
>   return r;
> }

The iterate could be done by putting a pointer (and most of the local variables) 
in the stack frame outside the foreach and having the iterate actually be 
a "store next op in ptr, return" and then placing a goto ptr at the start 
of the function. this would let the function return and resume at the same 
point on the next call (this is something like a generator I think). languages 
support needed big time.





More information about the Digitalmars-d mailing list