Feeds

back to article To iterate is human

In the previous article, I made the following observation: A collection that holds its elements but doesn't allow you to traverse them is unlikely to prove popular. There are many ways to offer traversal, but if the caller needs to be able to know the position of elements in some way there are essentially only three general …

COMMENTS

This topic is closed for new posts.

To enumerate, divine

I haven't been a regular Java user since 1.4, so it slipped my mind that, indeed, Java has specific syntax sugar for the Iterator. It didn't slip my mind that an internal iterator call would look frightful:

col.foreach(new Callable() { public void call(Object obj) {

// loop code goes here

}})

Verity Stob demonstrates in "Out of the (C++) loop", language idiom does (and should) depend on what's legible in the syntax of the language.

If Java 1.6 had closures, as you say, that would open the door for such things. (Is it just me, or is Java getting less nerfed as it goes along?)

I still dislike the idea of having two classes that are so completely interdependent as a Java Collection/Iterator pair. I also find an Iterator's usually (not most classes, most loops -- arrays/lists) an abstraction that adds complexity for no benefit.

And, as I pointed out earlier, 90% of the times you want to iterate over a loop, you do just want to run a bit of code against each object in the collection. The Enumeration pattern is the obvious way to do that without the caller having to worry about the details.

You mention Smalltalk; I believe that's where Ruby gets its iterator style from. And you mention Lisp's map inaccurately. Lisps have other functions that apply a function to each element of a list, notably foldl and family.

0
0

Linguistics

As time goes on it does indeed appear that Java is acquiring more powerful features, but it appears to be at the expense of consistent language design and invariably makes parts of the language more complex. Whether the additional complexity is worth it in all cases is questionable. For many recent and forthcoming features, it might be that Java is simply the wrong base language to extend, i.e. "I wouldn't start from here" :->

In terms of Ruby's iteration, the most obvious influences are Smalltalk, with blocks of code treated as objects, and CLU, with a coroutine-like ability to suspend execution of a block and yield a value (an approach that has since been incorporated into C#).

In Scheme, Enumeration Method is actually most like the for-each function, but as I was focusing on a more strictly functional programming perspective the map function was closer. The fold (or apply or reduce) family of functions found in languages such as Lisp, Scheme, Haskell, etc, is actually not a particularly representative example of an Enumeration Method since, for a single list, each application of the passed function receives two arguments rather than one: one is a list element and the other is the result of the previous application of the function on the previous element (and so on). Definitely useful, but not as direct or simple an example of a straight Enumeration Method.

0
0
This topic is closed for new posts.