www.digitalmars.com         C & C++   DMDScript  

D - add a "collection iterator" style for loop

reply "David Jeske" <dwj-d chat.net> writes:

right now I'd be testing it out.

One thing that seems to be omitted but which is extremely important is
including some type of loop construct which uses an iteration interface. For

datatypes should be able to use it.
Aug 16 2001
parent reply "Sean L. Palmer" <spalmer iname.com> writes:
I agree with this.  Something like:

char[] a = "blah";

for (char j in a)
{
  write(j);
}

On second thought it's just minor syntactic sugar for this:

char[] a = "blah";

for (int i=0; i<a.size; ++i)
{
  char j=a[i];
  write(j);
}

And the iteration probably can't guarantee order of traversal without making
speed sacrifices...  Then again, it'd keep the compiler from having to
optimize away all those multiplications by &a + i * char.size as the
compiler knows it's iterating an array and could just code up adds.

Also you'd want to keep in mind what might happen if the body of the
iterating for loop modified the container somehow... added or deleted a
portion maybe.  Sounds like it might be a standards nightmare.  ;)

Sean

"David Jeske" <dwj-d chat.net> wrote in message
news:9lic6v$q6u$1 digitaldaemon.com...

available
 right now I'd be testing it out.

 One thing that seems to be omitted but which is extremely important is
 including some type of loop construct which uses an iteration interface.
For

collection
 datatypes should be able to use it.
Oct 16 2001
parent reply "Walter" <walter digitalmars.com> writes:
I'm so used to the for (int i=0; i<a.length; i++) idiom, my mind sees it as
one long keyword <g>.

"Sean L. Palmer" <spalmer iname.com> wrote in message
news:9qgt4j$1n9r$1 digitaldaemon.com...
 I agree with this.  Something like:

 char[] a = "blah";

 for (char j in a)
 {
   write(j);
 }

 On second thought it's just minor syntactic sugar for this:

 char[] a = "blah";

 for (int i=0; i<a.size; ++i)
 {
   char j=a[i];
   write(j);
 }

 And the iteration probably can't guarantee order of traversal without
making
 speed sacrifices...  Then again, it'd keep the compiler from having to
 optimize away all those multiplications by &a + i * char.size as the
 compiler knows it's iterating an array and could just code up adds.

 Also you'd want to keep in mind what might happen if the body of the
 iterating for loop modified the container somehow... added or deleted a
 portion maybe.  Sounds like it might be a standards nightmare.  ;)

 Sean

 "David Jeske" <dwj-d chat.net> wrote in message
 news:9lic6v$q6u$1 digitaldaemon.com...

available
 right now I'd be testing it out.

 One thing that seems to be omitted but which is extremely important is
 including some type of loop construct which uses an iteration interface.
For

collection
 datatypes should be able to use it.
Dec 17 2001
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:9vmbr9$1hip$1 digitaldaemon.com...

 I'm so used to the for (int i=0; i<a.length; i++) idiom, my mind sees it
as
 one long keyword <g>.
But this won't work for associative arrays. And besides, if you ever consider adding linked lists into D (BTW a feature request!), you'll have to supply some way to iterate through the list quickly.
Dec 17 2001
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:9vmpk6$1q6h$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:9vmbr9$1hip$1 digitaldaemon.com...

 I'm so used to the for (int i=0; i<a.length; i++) idiom, my mind sees it
as
 one long keyword <g>.
But this won't work for associative arrays.
Associative arrays can be easilly converted to dynamic arrays, from which you can do the for loop idiom.
 And besides,
 if you ever consider adding linked lists into D (BTW a
 feature request!), you'll have to supply some way to
 iterate through the list quickly.
Linked lists already work: class Foo { Foo next; .... }
Dec 17 2001
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:9vmvvm$1v6v$1 digitaldaemon.com...

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:9vmpk6$1q6h$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:9vmbr9$1hip$1 digitaldaemon.com...

 I'm so used to the for (int i=0; i<a.length; i++) idiom, my mind sees
it
 as
 one long keyword <g>.
But this won't work for associative arrays.
Associative arrays can be easilly converted to dynamic arrays, from which you can do the for loop idiom.
How exactly do you do that? Like this? int[char[]] dict; int[] col = cast(int[]) dict; for (int i = 0; i < col.length; i++) ...
 And besides,
 if you ever consider adding linked lists into D (BTW a
 feature request!), you'll have to supply some way to
 iterate through the list quickly.
Linked lists already work: class Foo { Foo next; .... }
Actually I meant something like std::list. Speaking of syntax, since you can't have array of voids, it might be used as indicator for linked lists: struct vector { int x, y, z; } vector a, b; void[vector] list; // linked list of vectors list ~= a; list = list ~ b;
Dec 18 2001
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:9vnn2u$2dr5$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:9vmvvm$1v6v$1 digitaldaemon.com...

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:9vmpk6$1q6h$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:9vmbr9$1hip$1 digitaldaemon.com...

 I'm so used to the for (int i=0; i<a.length; i++) idiom, my mind
sees
 it
 as
 one long keyword <g>.
But this won't work for associative arrays.
Associative arrays can be easilly converted to dynamic arrays, from
which
 you can do the for loop idiom.
How exactly do you do that? Like this? int[char[]] dict; int[] col = cast(int[]) dict; for (int i = 0; i < col.length; i++) ...
That's an interesting idea! But the way it works now is to use the .keys property which returns an array of all the keys. Then, iterate over that array, plugging the values into the associative array to return the values.
 And besides,
 if you ever consider adding linked lists into D (BTW a
 feature request!), you'll have to supply some way to
 iterate through the list quickly.
Linked lists already work: class Foo { Foo next; .... }
Actually I meant something like std::list. Speaking of syntax, since you can't have array of voids, it might be used as indicator for linked lists: struct vector { int x, y, z; } vector a, b; void[vector] list; // linked list of vectors list ~= a; list = list ~ b;
I admit I find std::list confusing.
Dec 18 2001
parent Russell Borogove <kaleja estarcion.com> writes:
Walter wrote:
 But this won't work for associative arrays.
Associative arrays can be easilly converted to dynamic arrays, from
which
 you can do the for loop idiom.
How exactly do you do that? Like this? int[char[]] dict; int[] col = cast(int[]) dict; for (int i = 0; i < col.length; i++) ...
That's an interesting idea! But the way it works now is to use the .keys property which returns an array of all the keys. Then, iterate over that array, plugging the values into the associative array to return the values.
But some sort of foreach keyword would be easier to write, clearer, and easily extensible to any added builtin collection types. The only scary parts would be dealing with added/deleted elements during the loop. -RB
Dec 18 2001