Thread overview
add a "collection iterator" style for loop
Aug 17, 2001
David Jeske
Oct 16, 2001
Sean L. Palmer
Dec 18, 2001
Walter
Dec 18, 2001
Pavel Minayev
Dec 18, 2001
Walter
Dec 18, 2001
Pavel Minayev
Dec 18, 2001
Walter
Dec 18, 2001
Russell Borogove
August 17, 2001
D looks promising, and very much like C#. If there was a compiler 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 example, Python's "for XYZ in ABC:" or C#'s foreach. All standard collection datatypes should be able to use it.


October 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...
> D looks promising, and very much like C#. If there was a compiler
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
> example, Python's "for XYZ in ABC:" or C#'s foreach. All standard
collection
> datatypes should be able to use it.



December 18, 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 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...
> > D looks promising, and very much like C#. If there was a compiler
> 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
> > example, Python's "for XYZ in ABC:" or C#'s foreach. All standard
> collection
> > datatypes should be able to use it.
>
>
>


December 18, 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 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.


December 18, 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...
>
> > 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;
        ....
    }




December 18, 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...
> > "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;


December 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...
>
> > "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.


December 18, 2001
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