Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 25, 2003 foreach ideas | ||||
---|---|---|---|---|
| ||||
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. |
July 26, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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. > > |
July 26, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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
|
July 26, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Rasmussen | "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? |
July 27, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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: http://www.it.bton.ac.uk/staff/je/adacraft/ch06.htm#6.5 Ada is a beautiful language :) /David |
July 27, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Rasmussen | "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: > > http://www.it.bton.ac.uk/staff/je/adacraft/ch06.htm#6.5 Thanks, I see now. > Ada is a beautiful language :) Not sure I'd go that far <g>. |
July 28, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Rasmussen | > 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: > > http://www.it.bton.ac.uk/staff/je/adacraft/ch06.htm#6.5 > > Ada is a beautiful language :) > > /David > |
July 28, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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. |
August 04, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | Charles Sanders wrote:
>>Ada is a beautiful language :)
>
>
> Eww gross!
Why?
/David
|
August 05, 2003 Re: foreach ideas | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Rasmussen | "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. |
Copyright © 1999-2021 by the D Language Foundation