[squeak-dev] The Inbox: Collections-fbs.489.mcz

Frank Shearar frank.shearar at gmail.com
Mon Aug 20 19:00:37 UTC 2012


On 20 August 2012 18:29, Colin Putney <colin at wiresong.com> wrote:
> On Mon, Aug 20, 2012 at 10:04 AM, Frank Shearar <frank.shearar at gmail.com> wrote:
>> Another way to flatten without flattening would be to have an external
>> iterator: PreOrderTraversal on: myNestedCollection could define all
>> the usual collection methods, and use the underlying collections'
>> #do:. (And such an iterator could iterate over heterogeneous
>> structures too.)
>
> I quite like this approach. I used it in Filesystem, for walking
> directory trees. See FSPreorderGuide, FSPostorderGuide and
> FSBreadthFirstGuide. The term "Guide" comes from the fact that they
> work with Visitors, guiding them around the filesystem.

My Zipper library uses the same idea: take a look at
https://github.com/frankshearar/zipr/blob/master/lib/zipr/zipper.rb
from about line 373 onwards. (I would link to my Smalltalk zipper, but
its implementation isn't near as polished as the Ruby one. Clearly I
wrote the Ruby one after learning from my mistakes in implementing the
Smalltalk one!)

OK, it's a bit complicated by the fact that the various navigation
routines use a trampoline to iterate-ify a recursive method, and it
uses the Either monad for "try go left and if you can do this,
otherwise do that" logic, but the idea should still be quite clear:
it's handy to have things over which you can iterate providing basic
navigation - down, left, right and so on - and tying the bits together
with a more sophisticated traversal mechanism. Especially if said
traversal can map, fold and do all sorts of interesting things.

In FileSystem's case, I imagine that the various visitors say "this is
how you can iterate over the little bit of the overall structure I
represent". I've heard Visitor described as "Visitor = Iteration +
Double Dispatch."

frank

> Colin
>


More information about the Squeak-dev mailing list