Login Register

Collections getting some long-overdue love

I'm getting back to making fixes to the Collections, and there will be a number of breaking changes--namely with the Iterators (which are horribly broken). Both the Iterator and the DictionaryIterator will change the way elements are accessed. For example:
var a=someArrayList;   // type dojo.collections.ArrayList
var iterator=a.getIterator();
while(e.current() e.get()){
   doSomething(e.element);
}
The key here is the way you iterate; iterators have a new method called current get (note that this is a method that replaces the "current" property); this method returns the current element and then advances the internal cursor by one. If the iterator has reached the end of internal list, current() get() returns null (making it useful as a test, like above). There is also an atEnd method (an actual method, replacing the atEnd property) which allows you to check to see if the collection has finished iterating. The element on which the iterator is currently sitting is also accessible via the element property (see above); this is a convienence property more than anything else. Iterators also will support a map method, allowing you to do functional iteration instead:
var result=(a.getIterator()).map(doSomething);
These changes are still being worked on but will be checked in some time next week, so if you develop against the HEAD revision and are using Collections, be warned! (UPDATE: changing "current()" to "get()" per Bill Keese's suggestion. Thanks Bill!)

Hmm... old name and new

Hmm... old name and new implementation logic? Why do you not want to use one of well-known interfaces like java.util.Iterator as a base? For instance: var it = someCollection.getIterator(); while(it.hasNext()){ var element = it.next(); }

Ya, yuck. Most commensense

Ya, yuck. Most commensense interpretations of current() would expect the internal cursor to remain unchanged. There are well-established patterns for iteration, Python has a nice one that MochiKit follows. Why re-invent the wheel here, and especially with an oddball syntax? Rick

Rick: the consensus on the

Rick: the consensus on the lists is also that the name "current" is awful. Likewise the name "map" probably won't stand since what we *really* mean is "forEach" instead of "operate on these items in place". Thankfully these were just suggestions and not the implemented and blessed final names. Regards

I like the way Java API

I like the way Java API looks like. whiel (it.hasNext() {it.next().something(); } current() looks just bad.

Alex: Really? Activity on

Alex: Really? Activity on this morning's -interest mailing list look like this is a done deal. Tom: Please reconsider this and instead follow one of the established patterns. There won't be a second chance to break back-compatibility, and this API sounds awful. Rick

So how about a ruby/python

So how about a ruby/python like for-each loop? elements.each(function(el){});

See map(). The issue there

See map(). The issue there is that you have no real way of knowing that you're dealing with a value or reference, and the iterator supports that using map: var result=myArrayList.getIterator().map(function(el){}); If your collection is comprised of objects, then you will be altering the actual values within the collection. If the elements of the collection are values/primitives, then you can use the returned array of elements as your python-like loop. (the other difference is that it won't be raising an exception to stop the iteration).

PLEASE, do not call it

PLEASE, do not call it current() this is a total unlogic name, because it suggests something different. i suggenst "it.next()" which will incease the pointer by one and return the element or return false. This is simply, sounds right and works like expected.

Daniel, Lots of different

Daniel, Lots of different opinions which makes this a difficult thing to do. it.next() follows the Java paradigm; the original was based on the MS Enumerator object (which is a part of JScript); lots of arguments both ways. The thought to call it current() is because that's the primary purpose of the method; the advancement of the cursor is secondary to returning the current element of the iterator. This is the difference in thinking; Java's .next() implies that the purpose of the method is to advance the cursor. I'm not doing this to be different, I'm doing it to be very clear without carrying the baggage over from other languages (remember--Javascript is NOT Java). It's simple and logical despite the fact that it doesn't match Java. Bill Keese, however, suggested "get", and I think I'm going to change it to that; it's about as simple and clear as it can be. "map" is a standard name (not forEach), and it will follow dojo.lang.map as well as Moz's Array extras--so I don't think that's going to change either. forEach is a slightly different thing, and after looking at both I decided map is the better option.

please provide some inputs

please provide some inputs to do pagination and sorting of records in pagination. i am eagerly waiting for the same