Do you know a js library that implements a generic Iterator class for collections (be it Arrays or some abstract Enumerable) with a full set of features, like the Google Common or the Apache Commons?
Edit: Enumerable#each
is not an Iterator class. I'm looking for an Iterator, something that would let us write something like:
var iterator = new Iterator(myCollection);
for(var element = iterator.next(); iterator.hasNext(); element = iterator.next()) {
// iterator
}
Edit : mamoo reminded us of the Iterator implementation in Mozilla's Javascript 1.7. So the goal now is to find an implementation of this Iterator function in Javascript 1.5 (ECMA 4).
Edit2 : Why using an iterator when librairies (and ECMA 5) provide a each
method? First, because each
usually messes with this
because the callback is call
-ed (that's why each
accepts a second argument in Prototype). Then, because people are much more familiar with the for(;;)
construct than with the .each(callback)
construct (at least, in my field). Lastly, because an iterator can iterate over plain objects (see javascript 1.7).
Edit3 : I accepted npup's anwser, but here is my shot at it :
function Iterator(o, keysOnly) {
if(!(this instanceof arguments.callee))
return new arguments.callee(o, keysOnly);
var index = 0, keys = [];
if(!o || typeof o != "object") return;
if('splice' in o && 'join' in o) {
while(keys.length < o.length) keys.push(keys.length);
} else {
for(p in o) if(o.hasOwnProperty(p)) keys.push(p);
}
this.next = function next() {
if(index < keys.length) {
var key = keys[index++];
return keysOnly ? key : [key, o[key]];
} else throw { name: "StopIteration" };
};
this.hasNext = function hasNext() {
return index < keys.length;
};
}
var lang = { name: 'JavaScript', birthYear: 1995 };
var it = Iterator(lang);
while(it.hasNext()) {
alert(it.next());
}
//alert(it.next()); // A StopIteration exception is thrown
var langs = ['JavaScript', 'Python', 'C++'];
var it = Iterator(langs);
while(it.hasNext()) {
alert(it.next());
}
//alert(it.next()); // A StopIteration exception is thrown