D - foreach ideas
- Vathix (39/39) Jul 25 2003 user-defined foreach function:
- Walter (16/55) Jul 26 2003 Some great ideas! I've been thinking on the lines of:
- David Rasmussen (4/11) Jul 26 2003 Why not look at Ada's way of doing this? A nice generalized way of using...
- Walter (3/12) Jul 26 2003 Can you post a summary for those of us not familiar with Ada?
- David Rasmussen (11/18) Jul 27 2003 In general:
- Walter (4/13) Jul 27 2003 Thanks, I see now.
- Charles Sanders (4/23) Jul 28 2003 Eww gross!
- David Rasmussen (3/7) Aug 04 2003 Why?
- j anderson (3/10) Aug 04 2003 Beauty is in the eye of the beholder.
- David Rasmussen (3/21) Aug 05 2003 I know. I would like to know what his eye beholds.
- j anderson (16/37) Aug 06 2003 I'll bite (even though I'm not Sanders).
- Farmer (104/104) Jul 28 2003 Hi,
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
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 } ishit} } 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 youtoscan the items without having to make a (possibly) huge dynamic array ofthekeys or values. The restriction would be that you can't change the arrayorrehash during foreach.
Jul 26 2003
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
"David Rasmussen" <david.rasmussen gmx.net> wrote in message news:bfug1l$940$1 digitaldaemon.com...Walter wrote:Can you post a summary for those of us not familiar with Ada?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.
Jul 26 2003
Walter wrote: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 :) /DavidWhy 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 27 2003
"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
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: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 :) /DavidWhy 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 28 2003
Charles Sanders wrote:Why? /DavidAda is a beautiful language :)Eww gross!
Aug 04 2003
"David Rasmussen" <david.rasmussen gmx.net> wrote in message news:bglsug$1tel$1 digitaldaemon.com...Charles Sanders wrote:Beauty is in the eye of the beholder.Why? /DavidAda is a beautiful language :)Eww gross!
Aug 04 2003
j anderson wrote:"David Rasmussen" <david.rasmussen gmx.net> wrote in message news:bglsug$1tel$1 digitaldaemon.com...I know. I would like to know what his eye beholds. /DavidCharles Sanders wrote:Beauty is in the eye of the beholder.Why? /DavidAda is a beautiful language :)Eww gross!
Aug 05 2003
"David Rasmussen" <david.rasmussen gmx.net> wrote in message news:bgoc16$18tj$1 digitaldaemon.com...j anderson wrote: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."David Rasmussen" <david.rasmussen gmx.net> wrote in message news:bglsug$1tel$1 digitaldaemon.com...I know. I would like to know what his eye beholds. /DavidCharles Sanders wrote:Beauty is in the eye of the beholder.Why? /DavidAda is a beautiful language :)Eww gross!
Aug 06 2003
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