Empowering foreach with in-place iteration

Daniel Keep daniel.keep.lists at gmail.com
Sun Sep 9 03:03:03 PDT 2007


Again, this would be tricky to do.  Since the body of a loop is a
function, you can't call into it at a specific place.  It's also not
something that crops up very often.

Perhaps a better alternative might be to try for generators; that
(possibly combined with some kind of special aggregate declaration)
might solve the problem.

 string unescape(string s)
 {
     string r;
     foreach( c ; aggregate = s.iter )
     {
         if c == '\')
         {
             c = aggregate.next;
             r ~= f(c);
         }
         else
         {
             r ~= c;
         }
     }
     return r;
 }

As a bonus, this would make writing custom aggregates much easier, and
possibly allow for easier implementation of lockstep iteration.

On a side note, my PullDOM implementation actually allows for exactly
this sort of behaviour, so it's certainly something you can do right
now; you just need to construct the aggregate appropriately.

	-- Daniel



More information about the Digitalmars-d mailing list