[dojo-contributors] [feedback wanted] Preferred Iteration forms

Tom Trenka ttrenka at gmail.com
Sat Apr 1 13:01:28 EST 2006


Because the iterators were originally written in the style that you (and I,
aside from semantic differences) are talking about, and were changed because
of the whole "first item" style issue.  do...while worked ok but then part
of the bug with the iterator forced the check first.  No one is happy with
it, which is why this discussion is happening.

I do not want to use terms like "eof" and "scan"; both imply some sort of
iteration over a stream of bytes and that's not what the collections are
doing.  The Iterators for the collections are forward-only cursors, so I'm
not planning on (nor do I want to) support reversing or random access to
items within the collection using an iterator.  There are other devices for
that.

Looking over language/framework standards, I see that .NET uses an
IEnumerator interface, which supports a Current property, a MoveNext and a
Reset method.  The form generally used in C# looks like:

IEnumerator e=myCollection.GetEnumerator();
while(e.MoveNext()){
    e.Current.property=someValue;
}

Java 2 (v.1.4+) in the least supports an Iterator interface, with hasNext(),
next() and remove() methods, with an iterator() methodon most collections.
It uses the general form:

while(iterator.hasNext()){
    object=iterator.next();
}

Internally Python is apparently using a "next()" method on a lst until a
StopIteration exception (which is probably why Bob did it how he did with
Mochi).  I don't know Python at all yet, so I'm taking this one on hearsay
from IBM.  (http://www-128.ibm.com/developerworks/library/l-pycon.html)

---

The original iterators were based on the C# version of IEnumeration (I know
C# pretty well, rarely code in Java) in conjunction with the JScript
Enumerator object (which was written to support iterating over MS-specific
collections).  The difference between the C# and Java versions seem to be
when the first element is accessed;  hence the issues between the original
form vs. the changes made to it.

So.

A vote is called for.  Choose between the .NET version and the Java version,
and I will implement by Monday.  Personal preference: C# version.

trt

On 3/31/06, Morris Johns <morris_dojo_contributor at morrisjohns.com> wrote:
>
> Bill wrote:
> > OK.  three issues:
> > 1. You are planning to do as below, right?
> > >      > while(var item=iterator.next())
> > This won't work if any of the items are false or null.
>
> But if the iterator API also supports a more() or hasNext() or !eof() or
> !end() function then you use that when iterating collections that have items
> that evaluate to false in a boolean context.
>
>
> I suggest scan() rather than next() as a keyword. using next() in the java
> style is confusing to any for-loopist.
>
> scan would allow the following styles:
>
> while (var obj = it.scan()) {
> }
>
> while (it.more()) {
>   var value = it.scan();
> }
>
> Both of which are readable, and scan makes sense even if you have reversed
> or filtered the iterator.
> Only downside is that we are not 100% java compatible, but I can live with
> that.
>
> I think next() should only increment, and not return *anything*, to
> prevent confusion because of different expectations you have about next()
> depending upon your usual language.
>
> > 2. For name/value, you are not sure yet, but thinking maybe as below,
> right?
> > > while(var item=iterator.next()){
> > >    var key=item.key();
> > >    var value=item.value();
> > >    do something w/key
> > >    do something w/value
> >
> >Just kind of wordy.
>
> It isn't wordy, common usage would be:
> while(var item=iterator.next()){
> //    do something w/item
> }
>
> > The syntax below is simpler:
> > while(iterator.next()){
> > }
>
> I think many people would find 'next' unclear in the above context:
> while(iterator.next()){
>   iterator.current();
> }
>
> a) Unclear usage of 'next' to anyone used to for style iterator.
> b) Perhaps unobvious that the first call to next() after iterator
> initialisation doesnt actually go to the next item. Need a 'BOF' state.
>
> Looks to me (or anyone used to for style iteration) that the first value
> has been skipped - too easy to make mistakes.
>
>
> Bill asked (IM) why the dojo iterator was changed to use the style:
> if (...) {
>   do {
>   } while (...);
> }
> Anyone know of any technical reason?
> _______________________________________________
> dojo-contributors mailing list
> dojo-contributors at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-contributors
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.dojotoolkit.org/pipermail/dojo-contributors/attachments/20060401/ada31080/attachment.htm 


More information about the dojo-contributors mailing list