www.digitalmars.com         C & C++   DMDScript  

D - foreach ideas

reply "Vathix" <vathix dprogramming.com> writes:
user-defined foreach function:

foreach HWND allItems()
{
 uint i;
 for(i = 0; i != items.length; i++)
 {
  out items[i];   jumps to foreach usage body, jumps back when the } is hit
 }
}

usage:

this body is like a delegate that gets called on "out" in the foreach
function
foreach(foo in allItems)
{
 doSomething(foo);
}


built-in foreach for arrays:

foreach(bar in myArray)
{
 bar.classMember();
}

for associative arrays:

foreach(justKey in assocArray)
{
 bat(justKey);
}

foreach(justValue[] in assocArray)
{
 baz(justValue);
}

foreach(value[key] in assocArray)
{
 printf("'%.*s' = %d\n", key, value);
}


Maybe the user-defined foreach would just be easier left out and manually
use delegates? I like the idea for associative arrays, it would allow you to
scan the items without having to make a (possibly) huge dynamic array of the
keys or values. The restriction would be that you can't change the array or
rehash during foreach.
Jul 25 2003
next sibling parent reply "Walter" <walter digitalmars.com> writes:
Some great ideas! I've been thinking on the lines of:

    long[] a;
    foreach (long i; a)
    {
    }

which would iterate over each element of a[]. Using ; instead of 'in'
eliminates parsing ambiguities. 'a' could be an array, an associative array,
or a class object. If a class object, some functions would have to be
defined (like in operator overloading).

-Walter

"Vathix" <vathix dprogramming.com> wrote in message
news:bfre1d$9je$1 digitaldaemon.com...
 user-defined foreach function:

 foreach HWND allItems()
 {
  uint i;
  for(i = 0; i != items.length; i++)
  {
   out items[i];   jumps to foreach usage body, jumps back when the } is
hit
  }
 }

 usage:

 this body is like a delegate that gets called on "out" in the foreach
 function
 foreach(foo in allItems)
 {
  doSomething(foo);
 }


 built-in foreach for arrays:

 foreach(bar in myArray)
 {
  bar.classMember();
 }

 for associative arrays:

 foreach(justKey in assocArray)
 {
  bat(justKey);
 }

 foreach(justValue[] in assocArray)
 {
  baz(justValue);
 }

 foreach(value[key] in assocArray)
 {
  printf("'%.*s' = %d\n", key, value);
 }


 Maybe the user-defined foreach would just be easier left out and manually
 use delegates? I like the idea for associative arrays, it would allow you
to
 scan the items without having to make a (possibly) huge dynamic array of
the
 keys or values. The restriction would be that you can't change the array
or
 rehash during foreach.
Jul 26 2003
parent reply David Rasmussen <david.rasmussen gmx.net> writes:
Walter wrote:

 Some great ideas! I've been thinking on the lines of:
 
     long[] a;
     foreach (long i; a)
     {
     }
 
Why not look at Ada's way of doing this? A nice generalized way of using ranges. /David
Jul 26 2003
parent reply "Walter" <walter digitalmars.com> writes:
"David Rasmussen" <david.rasmussen gmx.net> wrote in message
news:bfug1l$940$1 digitaldaemon.com...
 Walter wrote:
 Some great ideas! I've been thinking on the lines of:

     long[] a;
     foreach (long i; a)
     {
     }
Why not look at Ada's way of doing this? A nice generalized way of using ranges.
Can you post a summary for those of us not familiar with Ada?
Jul 26 2003
parent reply David Rasmussen <david.rasmussen gmx.net> writes:
Walter wrote:
Why not look at Ada's way of doing this? A nice generalized way of using
ranges.
Can you post a summary for those of us not familiar with Ada?
In general: http://www.adapower.com/learn/ and for example: http://www.it.bton.ac.uk/staff/je/adacraft/ (look at looping and ranges) There are many other good Ada tutorials and books online. Specifically: Ada is a beautiful language :) /David
Jul 27 2003
next sibling parent "Walter" <walter digitalmars.com> writes:
"David Rasmussen" <david.rasmussen gmx.net> wrote in message
news:bg0gip$28i1$1 digitaldaemon.com...
 In general:

 http://www.adapower.com/learn/

 and for example:

 http://www.it.bton.ac.uk/staff/je/adacraft/

 (look at looping and ranges)

 There are many other good Ada tutorials and books online.

 Specifically:


Thanks, I see now.
 Ada is a beautiful language :)
Not sure I'd go that far <g>.
Jul 27 2003
prev sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
 Ada is a beautiful language :)
Eww gross! Charles "David Rasmussen" <david.rasmussen gmx.net> wrote in message news:bg0gip$28i1$1 digitaldaemon.com...
 Walter wrote:
Why not look at Ada's way of doing this? A nice generalized way of using
ranges.
Can you post a summary for those of us not familiar with Ada?
In general: http://www.adapower.com/learn/ and for example: http://www.it.bton.ac.uk/staff/je/adacraft/ (look at looping and ranges) There are many other good Ada tutorials and books online. Specifically: Ada is a beautiful language :) /David
Jul 28 2003
parent reply David Rasmussen <david.rasmussen gmx.net> writes:
Charles Sanders wrote:
Ada is a beautiful language :)
Eww gross!
Why? /David
Aug 04 2003
parent reply "j anderson" <anderson badmama.com.au.REMOVE> writes:
"David Rasmussen" <david.rasmussen gmx.net> wrote in message
news:bglsug$1tel$1 digitaldaemon.com...
 Charles Sanders wrote:
Ada is a beautiful language :)
Eww gross!
Why? /David
Beauty is in the eye of the beholder.
Aug 04 2003
parent reply David Rasmussen <david.rasmussen gmx.net> writes:
j anderson wrote:

 "David Rasmussen" <david.rasmussen gmx.net> wrote in message
 news:bglsug$1tel$1 digitaldaemon.com...
 
Charles Sanders wrote:

Ada is a beautiful language :)
Eww gross!
Why? /David
Beauty is in the eye of the beholder.
I know. I would like to know what his eye beholds. /David
Aug 05 2003
parent "j anderson" <anderson badmama.com.au.REMOVE> writes:
"David Rasmussen" <david.rasmussen gmx.net> wrote in message
news:bgoc16$18tj$1 digitaldaemon.com...
 j anderson wrote:

 "David Rasmussen" <david.rasmussen gmx.net> wrote in message
 news:bglsug$1tel$1 digitaldaemon.com...

Charles Sanders wrote:

Ada is a beautiful language :)
Eww gross!
Why? /David
Beauty is in the eye of the beholder.
I know. I would like to know what his eye beholds. /David
I'll bite (even though I'm not Sanders). These ada verse C++ *discussions* seem to appear regularly on this newsgroup. IMHO it's whatever bakes the cake. You send a message to a opengl group asking about a comparison between directX, and your sure to get a big discussion on how opengl is better. You send that to a directX group and you get how directX does so much more then openGL. D was targeted at C/C++ users, and therefore it uses heaps of C/C++ syntax. On the other hand I guess most people using D are looking for a better C++. Therefore, I *think* most of the people using D are pro C++ style in general. Now I'm sure to get a large amount of *not me* replies, from people who what to make D more like their favorite language. That's not to say that D has been influenced by many other languages other then C/C++. PS - Sorry for starting the obvious, and this is in no way meant to be an attack.
Aug 06 2003
prev sibling parent Farmer <itsFarmer. freenet.de> writes:
Hi,

I'm not sure whether I understood all your ideas correctly, so I try here to 
repeat how I understood them.

1)
Given this small example for user-defined foreach functions:

// A simple Map class like in c++ stl.
class Map
{
   foreach Object keys() // special attribut foreach
   {
      //this is just pseudo code
      Node node=keysHead;
      while (node != null) 
      {
         out node.key;  // "calls" the foreach body
         node=node.next();
      }
      return;
   }
   // returns the value for the given key
   Object element(Object key);
}

main()
{
   Map map=new Map("./state.map");

   // print all key value pairs of the map
   foreach (key in conf.keys)
   {
      printf("Key: %*.s - value: %*.s\n"
         , key.toString(), conf.element(key).toString());
   }
}


The compiler translates this example to code that has exactly the same 
semantics as this D code:

class Map
{
   Object __foreach_keys(delegate void(Object) __foreachBody)
   {
      Node node=keysHead;
      while (node != null) 
      {
         __foreachBody(current.key);  // call the foreach-body 
         node=node.next();
      }
      return;
   }
}

main()
{  
   Map map=new Map("./state.map");

   // the foreach-body is transformed to an nested function
   void __foreachBody(Object __assigned)
   {
      Object key=__assigned;  // compiler added this 
      printf("Key: %*.s - value: %*.s\n"
         , key.toString(), conf.element(key).toString());
   }

   conf.__foreach_keys(__foreachBody); // do the loop
}


2)
Looks like the foreach-statement is just syntax-sugar. 

But when you use gotos, or breaks in the foreach-body then some special 
treatement by the compiler is needed: Normally you cannot jump out of nested 
functions with goto, but for the compiler generated foreach-body that seems 
useful and possible to implement.


3)
For build-in arrays and associative arrays you need not specify an foreach 
function as there is a default one provided by the compiler. Possibly every 
Class could also provide an default iterator. 


I'm not sure whether you intended this, but I think, it might be useful:

4)
The foreach functions can have any parameters, e.g. to constrain the 
iteration. For example:

class Map
{
  // returns all values for a set of keys.
  foreach Object values(Object keys[]);
}

main()
{
   Map conf;

  // this line isn't proper D, but the meaning is obvious
   Object keys[]=["aa", "bb"];  

   foreach (Object value in conf.values(keys) )
      puts(value.toString);
}

5)
You can create non-member foreach functions, too: 

foreach int reverseLoop(int[] v)
{
   for (int i=v.length-1; i > 0; i--)
      out v[i];
}

main()
{
   int v[]=[1,2,3,4,5];
   int count;
   foreach (count in reverseLoop(v))
   {
      printf("%d ", count);
   }  
}



Regards,

Farmer.
Jul 28 2003