Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 12, 2003 Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Walter I really like the new foreach statement. I think it would be nice to be able to have multiple iteration parameters for struct and classes. For example: foreach(Type1 a; Type2 b; object) { } Where object has an apply fuction like this: int apply(int delegate(inout Type1, inout Type2) dg); |
September 12, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | "Patrick Down" <pat@codemoon.com> wrote in message news:Xns93F3E774467D7patcodemooncom@63.105.9.61... > > Walter I really like the new foreach statement. > I think it would be nice to be able to have > multiple iteration parameters for struct and classes. > > For example: > > foreach(Type1 a; Type2 b; object) > { > } > > Where object has an apply fuction like this: > > int apply(int delegate(inout Type1, inout Type2) dg); Hmm. That is an interesting idea. It would parse easier if the object came first: foreach(object; Type1 a; Type2 b) or perhaps: foreach(Type1 a, Type2 b; object) note the use of , instead of ; |
September 12, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> escreveu na mensagem news:bjrq5h$2b8j$1@digitaldaemon.com... > "Patrick Down" <pat@codemoon.com> wrote in message news:Xns93F3E774467D7patcodemooncom@63.105.9.61... > > > > Walter I really like the new foreach statement. > > I think it would be nice to be able to have > > multiple iteration parameters for struct and classes. > > > > For example: > > > > foreach(Type1 a; Type2 b; object) > > { > > } > > > > Where object has an apply fuction like this: > > > > int apply(int delegate(inout Type1, inout Type2) dg); > > Hmm. That is an interesting idea. It would parse easier if the object came first: > > foreach(object; Type1 a; Type2 b) > > or perhaps: > > foreach(Type1 a, Type2 b; object) > > note the use of , instead of ; Another question about foreach. It doesn't handle iterating over multiple containers, does it? I mean, can we compare two lists for equivalence? bool equivalent(IntList xs, IntList ys) { if (xs.count() == ys.count()) { foreach(int x; xs; int y; ys) { if (x != y) { return false; } } return true; } else { return false; } } I know this syntax doesn't work but this kind should be allowed, or else we'll need to provide external iterators for our classes. Best regards, Daniel Yokomiso. "If there are really men with white skin in big ships sailing across the Great Water from the east, why haven't they sent us smoke signals? The believers in the White Gods are just flakos who don't understand the basic principles of witch doctoring. We have nothing to fear from them." - Steve Franklin at /. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.514 / Virus Database: 312 - Release Date: 29/8/2003 |
September 12, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in news:bjrq5h$2b8j$1@digitaldaemon.com: > "Patrick Down" <pat@codemoon.com> wrote in message news:Xns93F3E774467D7patcodemooncom@63.105.9.61... >> >> Walter I really like the new foreach statement. >> I think it would be nice to be able to have >> multiple iteration parameters for struct and classes. >> >> For example: >> >> foreach(Type1 a; Type2 b; object) >> { >> } >> >> Where object has an apply fuction like this: >> >> int apply(int delegate(inout Type1, inout Type2) dg); > > Hmm. That is an interesting idea. It would parse easier if the object came first: > > foreach(object; Type1 a; Type2 b) > > or perhaps: > > foreach(Type1 a, Type2 b; object) I like this one better. |
September 12, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Yokomiso | "Daniel Yokomiso" <daniel_yokomiso@yahoo.com.br> wrote in message news:bjs7lf$2v4v$1@digitaldaemon.com... > Another question about foreach. It doesn't handle iterating over multiple containers, does it? I mean, can we compare two lists for equivalence? > > bool equivalent(IntList xs, IntList ys) { > if (xs.count() == ys.count()) { > foreach(int x; xs; int y; ys) { > if (x != y) { > return false; > } > } > return true; > } else { > return false; > } > } > > I know this syntax doesn't work but this kind should be allowed, or else we'll need to provide external iterators for our classes. Unfortunately, I don't see how that can work with the way foreach is done. Doing equivalence will probably have to be a member function of IntList. |
September 12, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | In article <Xns93F3E774467D7patcodemooncom@63.105.9.61>, Patrick Down says... >foreach(Type1 a; Type2 b; object) >{ >} > >Where object has an apply fuction like this: I don't understand why we would need this. Why not use the following: foreach (Object myObject; myContainer) { Type1 a = myObject.a; Type2 b = myObject.b; } --Benji Smith |
September 12, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Yokomiso | In article <bjs7lf$2v4v$1@digitaldaemon.com>, Daniel Yokomiso says... >Another question about foreach. It doesn't handle iterating over multiple containers, does it? I mean, can we compare two lists for equivalence? > > >bool equivalent(IntList xs, IntList ys) { > if (xs.count() == ys.count()) { > foreach(int x; xs; int y; ys) { > if (x != y) { > return false; > } > } > return true; > } else { > return false; > } >} Not quite as elegant... template PairArray(Type1,Type2) { struct PairIter { Type1[] arr1; Type2[] arr2; int apply(int delegate(inout Type1, inout Type2) dg) { int rtn = 0; for(int i = 0; i < arr1.length; ++i) { rtn = dg(arr1[i],arr[2]); if(rtn) break; } return rtn; } } PairIter PairUp(Type1[] arr1, Type2[] arr2) { PairIter rtn; rtn.arr1 = arr1; rtn.arr2 = arr2; return rtn; } } bool equivalent(IntList xs, IntList ys) { if (xs.count() == ys.count()) { foreach(int x, int y; instance PairArray(int,int).PairUp(xs,ys)) { if (x != y) { return false; } } return true; } else { return false; } } |
September 13, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | "Patrick Down" <Patrick_member@pathlink.com> escreveu na mensagem news:bjss2n$oqk$1@digitaldaemon.com... > In article <bjs7lf$2v4v$1@digitaldaemon.com>, Daniel Yokomiso says... > > >Another question about foreach. It doesn't handle iterating over multiple containers, does it? I mean, can we compare two lists for equivalence? > > > > > >bool equivalent(IntList xs, IntList ys) { > > if (xs.count() == ys.count()) { > > foreach(int x; xs; int y; ys) { > > if (x != y) { > > return false; > > } > > } > > return true; > > } else { > > return false; > > } > >} > > Not quite as elegant... > > template PairArray(Type1,Type2) > { > struct PairIter > { > Type1[] arr1; > Type2[] arr2; > > int apply(int delegate(inout Type1, inout Type2) dg) > { > int rtn = 0; > for(int i = 0; i < arr1.length; ++i) > { > rtn = dg(arr1[i],arr[2]); > if(rtn) > break; > } > return rtn; > } > } > > PairIter PairUp(Type1[] arr1, Type2[] arr2) > { > PairIter rtn; > rtn.arr1 = arr1; > rtn.arr2 = arr2; > > return rtn; > } > } > > bool equivalent(IntList xs, IntList ys) { > if (xs.count() == ys.count()) { > foreach(int x, int y; instance PairArray(int,int).PairUp(xs,ys)) { > if (x != y) { > return false; > } > } > return true; > } else { > return false; > } > } Yes, but your solution doesn't work with non-array containers, unless they provide external iteration. If we have a "BinaryTree" and a "Heap" each with a "apply" method, it's useless. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.516 / Virus Database: 313 - Release Date: 1/9/2003 |
September 13, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> escreveu na mensagem news:bjsqg7$mf8$1@digitaldaemon.com... > > "Daniel Yokomiso" <daniel_yokomiso@yahoo.com.br> wrote in message news:bjs7lf$2v4v$1@digitaldaemon.com... > > Another question about foreach. It doesn't handle iterating over multiple > > containers, does it? I mean, can we compare two lists for equivalence? > > > > bool equivalent(IntList xs, IntList ys) { > > if (xs.count() == ys.count()) { > > foreach(int x; xs; int y; ys) { > > if (x != y) { > > return false; > > } > > } > > return true; > > } else { > > return false; > > } > > } > > > > I know this syntax doesn't work but this kind should be allowed, or else we'll need to provide external iterators for our classes. > > Unfortunately, I don't see how that can work with the way foreach is done. Doing equivalence will probably have to be a member function of IntList. There are infinite (or some really big number) of possible operations involving two or more containers, so making them all member functions isn't possible. I guess we'll still have to provide external iterators. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.516 / Virus Database: 313 - Release Date: 1/9/2003 |
September 14, 2003 Re: Multiple parameter foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Yokomiso | "Daniel Yokomiso" <daniel_yokomiso@yahoo.com.br> wrote in news:bk06c0$2c86$1@digitaldaemon.com: > > Yes, but your solution doesn't work with non-array containers, unless they provide external iteration. If we have a "BinaryTree" and a "Heap" each with a "apply" method, it's useless. Yes, but how would Walter implement it? The apply fuction drives the iteration so how would two or more apply fuctions supply parameters to the delegate callback? It would have to be done with coroutines. |
Copyright © 1999-2021 by the D Language Foundation