D - add a "collection iterator" style for loop
- David Jeske (6/6) Aug 16 2001 D looks promising, and very much like C#. If there was a compiler availa...
- Sean L. Palmer (26/32) Oct 16 2001 I agree with this. Something like:
-
Walter
(5/38)
Dec 17 2001
I'm so used to the for (int i=0; i
- Pavel Minayev (7/9) Dec 17 2001 as
- Walter (9/19) Dec 17 2001 Associative arrays can be easilly converted to dynamic arrays, from whic...
- Pavel Minayev (15/35) Dec 18 2001 it
- Walter (8/46) Dec 18 2001 sees
- Russell Borogove (6/21) Dec 18 2001 But some sort of foreach keyword would be easier to write, clearer,
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
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...availableright 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.Forcollectiondatatypes should be able to use it.
Oct 16 2001
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 withoutmakingspeed 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...availableright 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.Forcollectiondatatypes should be able to use it.
Dec 17 2001
"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 itasone 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
"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...Associative arrays can be easilly converted to dynamic arrays, from which you can do the for loop idiom.I'm so used to the for (int i=0; i<a.length; i++) idiom, my mind sees itasone 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.Linked lists already work: class Foo { Foo next; .... }
Dec 17 2001
"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...it"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 seesHow exactly do you do that? Like this? int[char[]] dict; int[] col = cast(int[]) dict; for (int i = 0; i < col.length; i++) ...asAssociative arrays can be easilly converted to dynamic arrays, from which you can do the for loop idiom.one long keyword <g>.But this won't work for associative arrays.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;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 18 2001
"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...sees"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 minditwhichasAssociative arrays can be easilly converted to dynamic arrays, fromone long keyword <g>.But this won't work for associative arrays.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.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++) ...I admit I find std::list confusing.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;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 18 2001
Walter wrote: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. -RBwhichBut this won't work for associative arrays.Associative arrays can be easilly converted to dynamic arrays, fromThat'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.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++) ...
Dec 18 2001