14.4. Iterators in JavaScriptPython's iteration protocol is very useful. It makes it possible to easily handle large data sets that get reduced down to results of a reasonable size. In addition, the itertools module provides a number of extremely convenient functions. If you haven't checked out itertools in Python, you definitely should. Or, you can play around with MochiKit.Iter's extensive implementation of the same functions. To use iteration tools, however, you need to have iterators. Just as in Python, an iterator in JavaScript will define a next() method that will throw a StopIteration exception when it is done processing. An iterable object will either define an iter() method or use registerIteratorFactory to tell MochiKit how to get an iterator for specific kinds of objects. MochiKit.Iter itself uses registerIteratorFactory to allow JavaScript arrays to participate in the iteration protocol. For your own JavaScript code, you will likely just define iter on your prototype to return an iterator for your code. registerIteratorFactory is handy when you're trying to iterate over an object that is not directly in your control. 14.4.1. Making an IteratorHere is a simple example that lets you iterate over the parameters in a query string: Let's take a look at this in use: >>> qs = new queryObj("name=Hosty%20Most&city=South%20Barton"); [object Object] >>> i = iter(qs) [object Object] >>> i.next() ["name", "Hosty Most"] >>> i.next() ["city", "South Barton"] This is similar to how you define and use iterators in Python. 14.4.2. Functions from ItertoolsMochiKit has faithful reproductions of all your favorites from Python's itertools. The groupby function takes an iterable and a key function and returns an iterable where each item that comes out is a pair: the key, and an iterator for the values that match that key. The items are assumed to be sorted on that key in advance. Here is an example that exercises groupby: numbers = [2, 4, 6, 8, 10, 1, 3, 5, 7, 9]; i = groupby(numbers, function(val) { if (val % 2 == 0) { return "even"; } else { return "odd"; } }); try { while (info = i.next()) { writeln("Group: " + info[0]); j = info[1]; try { while (num = j.next()) { writeln(num); } } catch (e) { if (e != StopIteration) { throw e; } } } } catch (e) { if (e != StopIteration) { throw e; } } Here is the output you see when you run this in the interactive interpreter: Group: even 2 4 6 8 10 Group: odd 1 3 5 7 9 MochiKit also has a version of this that is not found in itertools. groupby_as_array will return an array of arrays rather than an iterator of iterators. Something that is found in Python, but not itertools specifically, is the list function. MochiKit.Iter implements this function for JavaScript. It returns a list (array) from an iterator. Another function that is a Python built-in but is found in MochiKit.Iter is reduce(func, i[, initial]). reduce returns a single value by calling func with either the last returned value or the initial value and i.next(). This makes it easy to compute a result based on a lot of data that you iterate over. The following functions are ones that MochiKit.Iter offers that are also in Python's itertools module or Python built-ins. These functions all return iterators, and the return values mentioned are what those iterators will return:
14.4.3. Functions Unique to MochiKit.IterAs useful as Python's itertools module is, MochiKit.Iter adds some other functions, too.
|