www.digitalmars.com         C & C++   DMDScript  

D - 'foreach' style loop?

reply James Yates <James_member pathlink.com> writes:
in some languages there is a 'foreach' statement. I was wondering if you would
(or plan to) implement the 'foreach' syntax in D. something like:

foreach ( char in szString )

you could use the 'in' syntax. Maybe this would depend too much on the datatype
used in the 'foreach' statement block...

just a thought
May 20 2002
parent reply "Walter" <walter digitalmars.com> writes:
"James Yates" <James_member pathlink.com> wrote in message
news:acba9v$q4o$1 digitaldaemon.com...
 in some languages there is a 'foreach' statement. I was wondering if you
would
 (or plan to) implement the 'foreach' syntax in D. something like:

 foreach ( char in szString )

 you could use the 'in' syntax. Maybe this would depend too much on the
datatype
 used in the 'foreach' statement block...

 just a thought
It's a good thought and many people have suggested it. It won't be in version 1, though, but maybe version 2.
May 20 2002
parent reply "anderson" <anderson firestar.com.au> writes:
That just gave me a thought (parhaps in the design for v3 or language E
;) ).
Parhaps foreach could be included in some type of set notation.

From here on I go into a bit of a ramble...

Ada (not that I like ada that much) definded a range type x..z.

range a = 0..20;
bit[a] b;
...

//Here are a couple of ideas

foreach (int n in a; n++) {} //The int and the in clash!

or
foreach (int n in b; n++) {}//The int and the in clash!

or
foreach (int n; a; n++) {}

or
foreach (int n; b; n++) {}

or (more c like)
for (int n = a; n++) {} //I like this version better then the "in", in this
case

(parhaps the step n++, could be optional like VB)

...
if (10 == a) ....

...
if (b[a] != 0) //If all values in range not = 0

int[a] c;
...
if (c[a] != 0) //If all values in range not = 0

Also in the interest of sets
you could and arrays together
ie
bit[a] e;
bit[a] f;
...
e = b & f;
//Which would be simular to a b U f , for bit arrays

e = b | f;
//Which would be simular to  a b n f, for bits arrays

(is that already included?)
In those cases a bit array would become like a set.

I'm probably just mumbling though.


"Walter" <walter digitalmars.com> wrote in message
news:acbbf4$r5b$2 digitaldaemon.com...
 "James Yates" <James_member pathlink.com> wrote in message
 news:acba9v$q4o$1 digitaldaemon.com...
 in some languages there is a 'foreach' statement. I was wondering if you
would
 (or plan to) implement the 'foreach' syntax in D. something like:

 foreach ( char in szString )

 you could use the 'in' syntax. Maybe this would depend too much on the
datatype
 used in the 'foreach' statement block...

 just a thought
It's a good thought and many people have suggested it. It won't be in version 1, though, but maybe version 2.
May 23 2002
parent reply "Sean L. Palmer" <seanpalmer earthlink.net> writes:
How about

class Foo;

{Foo} set_of_foo;
set_of_foo{}.transform(3);  // calls Foo::transform(3) on each member of foo
and tosses the results.
int{} foo_tests{} = set_of_foo{}.get_value(); // calls Foo::get_value() on
each foo and stores the results in foo_tests

Foo a;

if (a in set_of_foo) { printf("great, I found one"; }
if (set_of_foo.a) { printf("this syntax might work too"; }

operator ~ combines sets, ~= inserts into a set.  Still need intersection,
union, difference/removal operators.

intersection is like the binary and operator in some ways.  Like
multiplication also.
union is like the binary or operator in some ways.  Like addition with
saturation to 1.
difference is like subtraction with saturation to 0, which is like binary
nand.
Something's gotta be like subtraction or addition with wrapping, which is
like binary xor.

We need all the basic container types.  Array, set, singly and doubly linked
list, stack, hash, balanced or unbalanced tree.  All of which could be
fixed-size or dynamic, and could be compile-time literals, and probably all
using the [] operator somehow.

Sean

"anderson" <anderson firestar.com.au> wrote in message
news:acich4$2tat$1 digitaldaemon.com...
 That just gave me a thought (parhaps in the design for v3 or language E
 ;) ).
 Parhaps foreach could be included in some type of set notation.

 From here on I go into a bit of a ramble...

 Ada (not that I like ada that much) definded a range type x..z.

 range a = 0..20;
 bit[a] b;
 ...

 //Here are a couple of ideas

 foreach (int n in a; n++) {} //The int and the in clash!

 or
 foreach (int n in b; n++) {}//The int and the in clash!

 or
 foreach (int n; a; n++) {}

 or
 foreach (int n; b; n++) {}

 or (more c like)
 for (int n = a; n++) {} //I like this version better then the "in", in
this
 case

 (parhaps the step n++, could be optional like VB)

 ...
 if (10 == a) ....

 ...
 if (b[a] != 0) //If all values in range not = 0

 int[a] c;
 ...
 if (c[a] != 0) //If all values in range not = 0

 Also in the interest of sets
 you could and arrays together
 ie
 bit[a] e;
 bit[a] f;
 ...
 e = b & f;
 //Which would be simular to a b U f , for bit arrays

 e = b | f;
 //Which would be simular to  a b n f, for bits arrays

 (is that already included?)
 In those cases a bit array would become like a set.

 I'm probably just mumbling though.


 "Walter" <walter digitalmars.com> wrote in message
 news:acbbf4$r5b$2 digitaldaemon.com...
 "James Yates" <James_member pathlink.com> wrote in message
 news:acba9v$q4o$1 digitaldaemon.com...
 in some languages there is a 'foreach' statement. I was wondering if
you
 would
 (or plan to) implement the 'foreach' syntax in D. something like:

 foreach ( char in szString )

 you could use the 'in' syntax. Maybe this would depend too much on the
datatype
 used in the 'foreach' statement block...

 just a thought
It's a good thought and many people have suggested it. It won't be in version 1, though, but maybe version 2.
May 23 2002
next sibling parent "anderson" <anderson firestar.com.au> writes:
"Sean L. Palmer" <seanpalmer earthlink.net> wrote in message
news:acin0v$55m$1 digitaldaemon.com...
 How about

 class Foo;

 {Foo} set_of_foo;
 set_of_foo{}.transform(3);  // calls Foo::transform(3) on each member of
foo
 and tosses the results.
 int{} foo_tests{} = set_of_foo{}.get_value(); // calls Foo::get_value() on
 each foo and stores the results in foo_tests

 Foo a;

 if (a in set_of_foo) { printf("great, I found one"; }
 if (set_of_foo.a) { printf("this syntax might work too"; }
Actually I was just thinking about using "in" for searching as well. More simply put int[] A = {...}; if (10 in A) { printf("found it");) That's one reason not to use in in a foreach, (in is so useful). Note however parhaps something like this could be done with in. While (10 in A) {} or While (10 not in A) {} //Searches until it finds value (not index) 10 or for (n=0; 10 not in A; n++) {} I don't think that would brake the if search rule, because "if" would still have to search all of A but it'd be done inside the if. Of cause the while statement couldn't be done in parellel like the "if", so that'd create some complications.
 operator ~ combines sets, ~= inserts into a set.  Still need intersection,
 union, difference/removal operators.

 intersection is like the binary and operator in some ways.  Like
 multiplication also.
 union is like the binary or operator in some ways.  Like addition with
 saturation to 1.
 difference is like subtraction with saturation to 0, which is like binary
 nand.
 Something's gotta be like subtraction or addition with wrapping, which is
 like binary xor.

 We need all the basic container types.  Array, set, singly and doubly
linked
 list, stack, hash, balanced or unbalanced tree.  All of which could be
 fixed-size or dynamic, and could be compile-time literals, and probably
all
 using the [] operator somehow.

 Sean

 "anderson" <anderson firestar.com.au> wrote in message
 news:acich4$2tat$1 digitaldaemon.com...
 That just gave me a thought (parhaps in the design for v3 or language E
 ;) ).
 Parhaps foreach could be included in some type of set notation.

 From here on I go into a bit of a ramble...

 Ada (not that I like ada that much) definded a range type x..z.

 range a = 0..20;
 bit[a] b;
 ...

 //Here are a couple of ideas

 foreach (int n in a; n++) {} //The int and the in clash!

 or
 foreach (int n in b; n++) {}//The int and the in clash!

 or
 foreach (int n; a; n++) {}

 or
 foreach (int n; b; n++) {}

 or (more c like)
 for (int n = a; n++) {} //I like this version better then the "in", in
this
 case

 (parhaps the step n++, could be optional like VB)

 ...
 if (10 == a) ....

 ...
 if (b[a] != 0) //If all values in range not = 0

 int[a] c;
 ...
 if (c[a] != 0) //If all values in range not = 0

 Also in the interest of sets
 you could and arrays together
 ie
 bit[a] e;
 bit[a] f;
 ...
 e = b & f;
 //Which would be simular to a b U f , for bit arrays

 e = b | f;
 //Which would be simular to  a b n f, for bits arrays

 (is that already included?)
 In those cases a bit array would become like a set.

 I'm probably just mumbling though.


 "Walter" <walter digitalmars.com> wrote in message
 news:acbbf4$r5b$2 digitaldaemon.com...
 "James Yates" <James_member pathlink.com> wrote in message
 news:acba9v$q4o$1 digitaldaemon.com...
 in some languages there is a 'foreach' statement. I was wondering if
you
 would
 (or plan to) implement the 'foreach' syntax in D. something like:

 foreach ( char in szString )

 you could use the 'in' syntax. Maybe this would depend too much on
the
 datatype
 used in the 'foreach' statement block...

 just a thought
It's a good thought and many people have suggested it. It won't be in version 1, though, but maybe version 2.
May 23 2002
prev sibling parent "anderson" <anderson firestar.com.au> writes:
"Sean L. Palmer" <seanpalmer earthlink.net> wrote in message
news:acin0v$55m$1 digitaldaemon.com...
 How about

 class Foo;

 {Foo} set_of_foo;
 set_of_foo{}.transform(3);  // calls Foo::transform(3) on each member of
foo
 and tosses the results.
 int{} foo_tests{} = set_of_foo{}.get_value(); // calls Foo::get_value() on
 each foo and stores the results in foo_tests

 Foo a;

 if (a in set_of_foo) { printf("great, I found one"; }
 if (set_of_foo.a) { printf("this syntax might work too"; }

 operator ~ combines sets, ~= inserts into a set.  Still need intersection,
 union, difference/removal operators.

 intersection is like the binary and operator in some ways.  Like
 multiplication also.
 union is like the binary or operator in some ways.  Like addition with
 saturation to 1.
 difference is like subtraction with saturation to 0, which is like binary
 nand.
 Something's gotta be like subtraction or addition with wrapping, which is
 like binary xor.

 We need all the basic container types.  Array, set, singly and doubly
linked
 list, stack, hash, balanced or unbalanced tree.  All of which could be
 fixed-size or dynamic, and could be compile-time literals, and probably
all
 using the [] operator somehow.
They would be extremly useful. How many times have I used glPushMatrix now...
 Sean

 "anderson" <anderson firestar.com.au> wrote in message
 news:acich4$2tat$1 digitaldaemon.com...
 That just gave me a thought (parhaps in the design for v3 or language E
 ;) ).
 Parhaps foreach could be included in some type of set notation.

 From here on I go into a bit of a ramble...

 Ada (not that I like ada that much) definded a range type x..z.

 range a = 0..20;
 bit[a] b;
 ...

 //Here are a couple of ideas

 foreach (int n in a; n++) {} //The int and the in clash!

 or
 foreach (int n in b; n++) {}//The int and the in clash!

 or
 foreach (int n; a; n++) {}

 or
 foreach (int n; b; n++) {}

 or (more c like)
 for (int n = a; n++) {} //I like this version better then the "in", in
this
 case

 (parhaps the step n++, could be optional like VB)

 ...
 if (10 == a) ....

 ...
 if (b[a] != 0) //If all values in range not = 0

 int[a] c;
 ...
 if (c[a] != 0) //If all values in range not = 0

 Also in the interest of sets
 you could and arrays together
 ie
 bit[a] e;
 bit[a] f;
 ...
 e = b & f;
 //Which would be simular to a b U f , for bit arrays

 e = b | f;
 //Which would be simular to  a b n f, for bits arrays

 (is that already included?)
 In those cases a bit array would become like a set.

 I'm probably just mumbling though.


 "Walter" <walter digitalmars.com> wrote in message
 news:acbbf4$r5b$2 digitaldaemon.com...
 "James Yates" <James_member pathlink.com> wrote in message
 news:acba9v$q4o$1 digitaldaemon.com...
 in some languages there is a 'foreach' statement. I was wondering if
you
 would
 (or plan to) implement the 'foreach' syntax in D. something like:

 foreach ( char in szString )

 you could use the 'in' syntax. Maybe this would depend too much on
the
 datatype
 used in the 'foreach' statement block...

 just a thought
It's a good thought and many people have suggested it. It won't be in version 1, though, but maybe version 2.
May 23 2002