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

Sandro Magi smagi at naasking.homeip.net
Mon Apr 3 14:02:07 EDT 2006

Tom Trenka wrote:

>Sorry, emailed it just to Alex first.
>---------- Forwarded message ----------
>From: Tom Trenka <ttrenka at gmail.com>
>Date: Apr 3, 2006 10:21 AM
>Subject: Re: [dojo-contributors] [feedback wanted] Preferred Iteration forms
>To: alex at dojotoolkit.org
>On any of the Array based Collections, you can *always* call toArray and use
>the dojo.lang array functions to do functional iteration.  In theory you can
>do the same with map-based collections by grabbing the KeyList from it and
>using dojo.lang.forEach for it.
>Why would I want to duplicate that functionality if you've already written

If this is the approach you're taking, then why add yet more iteration 
functionality if it's already there? Isn't that just needlessly 
expanding the codebase?

Instead of adding new, untested functions, you can trivially support 
functional iteration by using toArray and forEach yourself within the 
collections. Seems like a two-liner which is trivially testable, and 
gets you all the way there in terms of iteration functionality:

function forEach(coll, unary_func) {
  dojo.lang.forEach(coll.toArray(), unary_func, true);

Extra bonus: since you've copied the elements (or keys) to an array, you 
can mutate the collection while iterating over it.


More information about the dojo-contributors mailing list