For loop with separator

Q. Schroll qs.il.paperinik at gmail.com
Thu Jul 4 17:00:33 UTC 2019


Probably you've come over this problem once in a while, too.
You have a repeating solution, so you use a for(each) loop.
Sometimes, there is an action to be performed between the end of 
one iteration and the beginning of the next, if there is one. The 
prime example is printing the comma when printing a list: There 
is one between any two elements, but neither is one at front or 
behind the last one.

Typical solutions I employed were:
1 Handling the first element separately
2 Condition in the loop, that is false exactly for the first 
iteration.

1 can be done with ranges easily:

if (!range.empty)
{
     action(range.front);
     range.popFront;
     foreach (element; range)
     {
         betweenAction();
         action(element);
     }
}

This approach is clearly quite verbose for the problem, but 
there's nothing done unnecessarily.

2 can be done easily, too:

foreach (i, element; range)
{
     if (i > 0) betweenAction();
     action(element);
}

While 2 is less code, it's prone to be checked every iteration.
Note that 2 is rather D specific in its length. It can be done in 
other languages, but is more verbose.

Is there a cleaner solution that I missed?


More information about the Digitalmars-d-learn mailing list