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

Morris Johns morris_dojo_contributor at morrisjohns.com
Mon Apr 3 00:14:36 EDT 2006

A retraction! I spent a few hours considering implementation of iterators in JS and realised that my previous imperative experience doesn't translate.

An imperative style iterator is really not nice for accessing the values of an object. This is because JS implementation would require making a copy of all the keys into an array. The same problem can occur with arrays because JS arrays can be sparse and may contain key/value pairs. Collection classes regularly use objects as part of the implementation (e.g. a directory structure modelled using key/value pairs with key for subdirectory or file name and value as subdirectory object or file object).

An imperative style iteration API can be provided, even though it is likely to have efficiency compromises.

That said, I have not had time to look at efficiency issues for the functional style, but it *should* be good since it can choose to use native constructs. (Aside: I don't think closures are problematic for iterator usage - using the closure's variables feels natural for this usage (*presuming* that this is a required parameter to the forEach function... Also only one closure occurs per complete iteration loop so unlikely to be problematic for efficiency).  


Are there any technical/efficiency reasons against using functional style iteration?

Tom, I get the feeling you are trying to avoid 'scope creep'! You have unfortunately chosen a fundamental topic to work on, implementation compromises are difficult, opinions are strong, and everybody has an opinion! I presumed you were implementing a generic iterator, yet you talk about a specific need. Tom, can you give us a better idea of the scope you want to keep within?

More information about the dojo-contributors mailing list