access foreach index counter for Iterate n times
mw
mingwu at gmail.com
Sat May 21 02:23:28 UTC 2022
On Saturday, 21 May 2022 at 02:08:05 UTC, Ali Çehreli wrote:
> 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);
Sure, all nice workarounds.
But isn't iota as a function call also expensive?
I wrote this naturally after seeing the array foreach example:
```
foreach (i, e; 5 .. 10)
```
I don't even know the function iota, and why it's needed for a
simple loop like this?
```
foreach (e; 5 .. 10)
```
just to get i?
My point: the language should be more consistent.
More information about the Digitalmars-d
mailing list