![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I've been playing with C# v2.0 today, and having quite a bit of fun. The new version has added iterators. The iterators are coded directly into the class to be iterated. For example: public IEnumerable<int> Visit(SomeVisitor visitor) { int position = 0; for(int i = 0; i < Count; i++) { yield return position; data[i].Accept(visitor); position++; } } This can then be used in a foreach construct: foreach(int position in someObject.Visit(this)) { // Do something with position here. } Where 'someObject' is the object to traverse, the class of which contains the iterator code above, and 'this' is an instance of a class derived from 'SomeVisitor.' So as the object is traversed, the iterator first returns the position information. Here it is just a simple integer, but it could be anything, something much more complex, perhaps. Then the Accept method is called. The idea is that you can traverse a collection where the Visitor design pattern is used without having to explicitely call the Accept method; the iterator takes care of that for you. Plus, you get additional information about the traversal that can be used as well. Care would have to be taken so that you know which comes first, the value returned from the iterator or the Accept method being called. But it seems worth the effort. This can be coded in different ways in other languages to the same effect, but I thought this use of C# v2.0 iterators together with the Visitor pattern was kinda neat. |
#3
| |||
| |||
|
|
You might also be interested in using iterators to explore graphs in various traversal patterns (depth first, breadth first, etc). I have coded examples in http://www.frontiernet.net/~fredm/dps/Contents.htm "Wavemaker" <jabberdabber (AT) BiteMeHotmail (DOT) com> wrote in message news:65mdnT6ScuIlnunfRVn-1Q (AT) comcast (DOT) com... I've been playing with C# v2.0 today, and having quite a bit of fun. The new version has added iterators. The iterators are coded directly into the class to be iterated. For example: public IEnumerable<int> Visit(SomeVisitor visitor) { int position = 0; for(int i = 0; i < Count; i++) { yield return position; data[i].Accept(visitor); position++; } } This can then be used in a foreach construct: foreach(int position in someObject.Visit(this)) { // Do something with position here. } Where 'someObject' is the object to traverse, the class of which contains the iterator code above, and 'this' is an instance of a class derived from 'SomeVisitor.' So as the object is traversed, the iterator first returns the position information. Here it is just a simple integer, but it could be anything, something much more complex, perhaps. Then the Accept method is called. The idea is that you can traverse a collection where the Visitor design pattern is used without having to explicitely call the Accept method; the iterator takes care of that for you. Plus, you get additional information about the traversal that can be used as well. Care would have to be taken so that you know which comes first, the value returned from the iterator or the Accept method being called. But it seems worth the effort. This can be coded in different ways in other languages to the same effect, but I thought this use of C# v2.0 iterators together with the Visitor pattern was kinda neat. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |