access foreach index counter for Iterate n times

Ali Çehreli acehreli at yahoo.com
Sat May 21 02:08:05 UTC 2022


On 5/20/22 18:26, forkit wrote:

> {
>      foreach(i, e; iota(5,11).array)
>          writeln(i, ":", e);
> }

That .array may be unnecessarily expensive in some cases because it 
allocates memory. The following is an alternative:

     foreach(i, e; iota(5,11).enumerate)
         writeln(i, ":", e);

Ali


More information about the Digitalmars-d mailing list