For loop with separator

Alex sascha.orlov at gmail.com
Thu Jul 4 17:46:24 UTC 2019


On Thursday, 4 July 2019 at 17:00:33 UTC, Q. Schroll wrote:
> 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?

As far as I can interpret it, joiner

https://dlang.org/library/std/algorithm/iteration/joiner.html

uses roughly the first approach.


More information about the Digitalmars-d-learn mailing list