Jump to page: 1 2 3
Thread overview
access foreach index counter for Iterate n times
May 21, 2022
mw
May 21, 2022
forkit
May 21, 2022
Ali Çehreli
May 21, 2022
mw
May 21, 2022
max haughton
May 21, 2022
Mike Parker
May 21, 2022
mw
May 21, 2022
Nick Treleaven
May 21, 2022
Ali Çehreli
May 21, 2022
mw
May 21, 2022
Adam D Ruppe
May 21, 2022
mw
May 21, 2022
user1234
May 21, 2022
Adam D Ruppe
May 21, 2022
mw
May 21, 2022
Adam D Ruppe
May 21, 2022
mw
May 21, 2022
rikki cattermole
May 21, 2022
mw
May 21, 2022
H. S. Teoh
May 21, 2022
Adam D Ruppe
May 21, 2022
mw
May 21, 2022
Adam D Ruppe
May 21, 2022

Not sure if this has been discussed before:

https://tour.dlang.org/tour/en/basics/foreach

foreach (i, e; [4, 5, 6]) {
    writeln(i, ":", e);
}
// 0:4 1:5 2:6

so what if I want the loop index counter when I want to loop n times with the .. syntax, I tried:

foreach (i, e; 5 .. 10) {
    writeln(i, ":", e);
}

right now when I tried it, got syntax error:

onlineapp.d(4): Error: found .. when expecting )
onlineapp.d(4): Error: found ) when expecting ; following statement

Yes, there are a number of manual work-around, but I think the this 2nd foreach loop should be as valid as the 1st one on arrays, so the language behavior is more consistent.

Thoughts?

May 21, 2022
On Saturday, 21 May 2022 at 01:01:40 UTC, mw wrote:
> Not sure if this has been discussed before:
>
> https://tour.dlang.org/tour/en/basics/foreach
>
> ```
> foreach (i, e; [4, 5, 6]) {
>     writeln(i, ":", e);
> }
> // 0:4 1:5 2:6
> ```
>
> so what if I want the loop index counter when I want to loop n times with the .. syntax, I tried:
>
> ```
> foreach (i, e; 5 .. 10) {
>     writeln(i, ":", e);
> }
> ```
>
> right now when I tried it, got syntax error:
>
> onlineapp.d(4): Error: found `..` when expecting `)`
> onlineapp.d(4): Error: found `)` when expecting `;` following statement
>
> Yes, there are a number of manual work-around, but I think the this 2nd foreach loop should be as valid as the 1st one on arrays, so the language behavior is more consistent.
>
> Thoughts?

// ------

module test;
@safe:

import std;

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

/+
0:5
1:6
2:7
3:8
4:9
5:10
+/

// ----------------------

May 20, 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
May 21, 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.
May 21, 2022
On Saturday, 21 May 2022 at 02:23:28 UTC, mw wrote:
> But isn't iota as a function call also expensive?

No.

https://d.godbolt.org/z/TsKbe96dv
> 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.

I don't disagree.
May 21, 2022
On Saturday, 21 May 2022 at 02:23:28 UTC, mw wrote:

> My point: the language should be more consistent.

You get indexes in a foreach with arrays and associative arrays because they have indexes. You don't get them with input ranges because they have no indexes (hence, the need for enumerate). So is it really in consistent that a numeric range of 0 .. N has no index?
May 20, 2022
On 5/20/22 19:23, mw wrote:

> But isn't iota as a function call also expensive?

I am sure someone will show some disassembly to compare. :)

> I wrote this naturally after seeing the array foreach example:
>
> ```
> foreach (i, e; 5 .. 10)
> ```

The biggest issue there is the number range, which is a weird thing in the language.

> My point: the language should be more consistent.

The automatic loop counter is available only for arrays. If we see number ranges things other than arrays (they are not arrays), then there is no consistency issue.

One could argue to remove number ranges from the language but I don't think it's that big of a deal.

Ali

May 21, 2022
On Saturday, 21 May 2022 at 02:35:37 UTC, Mike Parker wrote:
> On Saturday, 21 May 2022 at 02:23:28 UTC, mw wrote:
>
>> My point: the language should be more consistent.
>
> You get indexes in a foreach with arrays and associative arrays because they have indexes. You don't get them with input ranges because they have no indexes (hence, the need for enumerate). So is it really in consistent that a numeric range of 0 .. N has no index?

More abstractly, it's not array/range index, it's a loop counter (index).

May 21, 2022
On Saturday, 21 May 2022 at 02:40:06 UTC, Ali Çehreli wrote:
> On 5/20/22 19:23, mw wrote:
>
> > But isn't iota as a function call also expensive?
>
> I am sure someone will show some disassembly to compare. :)
>

from Max:

> https://d.godbolt.org/z/TsKbe96dv

Nice assembly.



>
> The biggest issue there is the number range, which is a weird thing in the language.
>
> > My point: the language should be more consistent.
>
> The automatic loop counter is available only for arrays. If we see number ranges things other than arrays (they are not arrays), then there is no consistency issue.

Yes! let's call it "loop counter", regardless the user is looping over array, or (number) range ...: so, "loop counter" as a language concept.


> One could argue to remove number ranges from the language but I don't think it's that big of a deal.


Why not keep it, and the compiler auto translate

foreach (i, e; 5 .. 10)

to:

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


esp. after seeing the assembly.
May 21, 2022
On Saturday, 21 May 2022 at 03:01:39 UTC, mw wrote:
> Yes! let's call it "loop counter"

It isn't a loop counter. It is an index back into the original source. Consider:

foreach(INDEX, dchar character; "“”")
   writeln(INDEX); // 0 then 3

« First   ‹ Prev
1 2 3