On 12/22/05, Michele Dondi <[EMAIL PROTECTED]> wrote: > Suppose I want to navigate a tree and print out info contained in each of > its leaves along with info gathered from the position in the tree of the > list itself? Can I do it in a "universal" manner as hinted above that > would work for other situations as well?
In Ruby, each collection class that wishes to be iterated over mixes in the Enumerable class. In a crude way, you can almost use that as the definition of what makes an class a collection. All the class has to provide is each(), which is a method that, according to the Ruby book, "yields successive members of the collection." If the class wishes to use the sorting items, then it has to provide a meaningful spaceship operator. each() (and a number of other methods) then takes a block that it executes at every node. As everything in Perl6 will be an object, I think we should be looking at Ruby for possible solutions to this kind of problem. The Ruby way, imho, would be to provide a set of "each()" methods that you can choose to use, depending on how you wanted to iterate over the object. The each() method you choose could maintain some kind of state about where the element is in the collection. Or, and this would be my preference, the elements should have the ability to return that state when queried. This implication here is that there should be an Enumerable role (or set of roles) in Perl6 that provides this kind of capability. In addition, there should probably be a useful set of base classes to do things like Set, Bag, Tree, etc. Then, if I wanted to create a 3-2 tree which only takes values that is-a Floober and default to iterating over it in level-order, I can do that in less than 20 lines of code within a class. Then, I use that class when I want to use my arbitrary data structure. The further implication of this is that I don't think we will be using AoHoAoHo... in applications anymore. In 1-off scripts, sure! But, we'll have the infrastructure to create usable classes that abstract away 90% of the complexity of our data structures, allowing us to focus on the similarities in our code vs. the differences. Ruby takes duck-typing to a religious mantra, which can be annoying, but it's definitely very productive. Perl6 could do worse than copying that to a large degree. Of course, this doesn't work for arbitrary complex structures, but I don't think anything would work for those. Rob