michael@0: /*** michael@0: michael@0: MochiKit.Iter 1.4.2 michael@0: michael@0: See for documentation, downloads, license, etc. michael@0: michael@0: (c) 2005 Bob Ippolito. All rights Reserved. michael@0: michael@0: ***/ michael@0: michael@0: MochiKit.Base._deps('Iter', ['Base']); michael@0: michael@0: MochiKit.Iter.NAME = "MochiKit.Iter"; michael@0: MochiKit.Iter.VERSION = "1.4.2"; michael@0: MochiKit.Base.update(MochiKit.Iter, { michael@0: __repr__: function () { michael@0: return "[" + this.NAME + " " + this.VERSION + "]"; michael@0: }, michael@0: toString: function () { michael@0: return this.__repr__(); michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.registerIteratorFactory */ michael@0: registerIteratorFactory: function (name, check, iterfactory, /* optional */ override) { michael@0: MochiKit.Iter.iteratorRegistry.register(name, check, iterfactory, override); michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.isIterable */ michael@0: isIterable: function(o) { michael@0: return o != null && michael@0: (typeof(o.next) == "function" || typeof(o.iter) == "function"); michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.iter */ michael@0: iter: function (iterable, /* optional */ sentinel) { michael@0: var self = MochiKit.Iter; michael@0: if (arguments.length == 2) { michael@0: return self.takewhile( michael@0: function (a) { return a != sentinel; }, michael@0: iterable michael@0: ); michael@0: } michael@0: if (typeof(iterable.next) == 'function') { michael@0: return iterable; michael@0: } else if (typeof(iterable.iter) == 'function') { michael@0: return iterable.iter(); michael@0: /* michael@0: } else if (typeof(iterable.__iterator__) == 'function') { michael@0: // michael@0: // XXX: We can't support JavaScript 1.7 __iterator__ directly michael@0: // because of Object.prototype.__iterator__ michael@0: // michael@0: return iterable.__iterator__(); michael@0: */ michael@0: } michael@0: michael@0: try { michael@0: return self.iteratorRegistry.match(iterable); michael@0: } catch (e) { michael@0: var m = MochiKit.Base; michael@0: if (e == m.NotFound) { michael@0: e = new TypeError(typeof(iterable) + ": " + m.repr(iterable) + " is not iterable"); michael@0: } michael@0: throw e; michael@0: } michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.count */ michael@0: count: function (n) { michael@0: if (!n) { michael@0: n = 0; michael@0: } michael@0: var m = MochiKit.Base; michael@0: return { michael@0: repr: function () { return "count(" + n + ")"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: m.counter(n) michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.cycle */ michael@0: cycle: function (p) { michael@0: var self = MochiKit.Iter; michael@0: var m = MochiKit.Base; michael@0: var lst = []; michael@0: var iterator = self.iter(p); michael@0: return { michael@0: repr: function () { return "cycle(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: try { michael@0: var rval = iterator.next(); michael@0: lst.push(rval); michael@0: return rval; michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: if (lst.length === 0) { michael@0: this.next = function () { michael@0: throw self.StopIteration; michael@0: }; michael@0: } else { michael@0: var i = -1; michael@0: this.next = function () { michael@0: i = (i + 1) % lst.length; michael@0: return lst[i]; michael@0: }; michael@0: } michael@0: return this.next(); michael@0: } michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.repeat */ michael@0: repeat: function (elem, /* optional */n) { michael@0: var m = MochiKit.Base; michael@0: if (typeof(n) == 'undefined') { michael@0: return { michael@0: repr: function () { michael@0: return "repeat(" + m.repr(elem) + ")"; michael@0: }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: return elem; michael@0: } michael@0: }; michael@0: } michael@0: return { michael@0: repr: function () { michael@0: return "repeat(" + m.repr(elem) + ", " + n + ")"; michael@0: }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: if (n <= 0) { michael@0: throw MochiKit.Iter.StopIteration; michael@0: } michael@0: n -= 1; michael@0: return elem; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.next */ michael@0: next: function (iterator) { michael@0: return iterator.next(); michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.izip */ michael@0: izip: function (p, q/*, ...*/) { michael@0: var m = MochiKit.Base; michael@0: var self = MochiKit.Iter; michael@0: var next = self.next; michael@0: var iterables = m.map(self.iter, arguments); michael@0: return { michael@0: repr: function () { return "izip(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { return m.map(next, iterables); } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.ifilter */ michael@0: ifilter: function (pred, seq) { michael@0: var m = MochiKit.Base; michael@0: seq = MochiKit.Iter.iter(seq); michael@0: if (pred === null) { michael@0: pred = m.operator.truth; michael@0: } michael@0: return { michael@0: repr: function () { return "ifilter(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: while (true) { michael@0: var rval = seq.next(); michael@0: if (pred(rval)) { michael@0: return rval; michael@0: } michael@0: } michael@0: // mozilla warnings aren't too bright michael@0: return undefined; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.ifilterfalse */ michael@0: ifilterfalse: function (pred, seq) { michael@0: var m = MochiKit.Base; michael@0: seq = MochiKit.Iter.iter(seq); michael@0: if (pred === null) { michael@0: pred = m.operator.truth; michael@0: } michael@0: return { michael@0: repr: function () { return "ifilterfalse(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: while (true) { michael@0: var rval = seq.next(); michael@0: if (!pred(rval)) { michael@0: return rval; michael@0: } michael@0: } michael@0: // mozilla warnings aren't too bright michael@0: return undefined; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.islice */ michael@0: islice: function (seq/*, [start,] stop[, step] */) { michael@0: var self = MochiKit.Iter; michael@0: var m = MochiKit.Base; michael@0: seq = self.iter(seq); michael@0: var start = 0; michael@0: var stop = 0; michael@0: var step = 1; michael@0: var i = -1; michael@0: if (arguments.length == 2) { michael@0: stop = arguments[1]; michael@0: } else if (arguments.length == 3) { michael@0: start = arguments[1]; michael@0: stop = arguments[2]; michael@0: } else { michael@0: start = arguments[1]; michael@0: stop = arguments[2]; michael@0: step = arguments[3]; michael@0: } michael@0: return { michael@0: repr: function () { michael@0: return "islice(" + ["...", start, stop, step].join(", ") + ")"; michael@0: }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: var rval; michael@0: while (i < start) { michael@0: rval = seq.next(); michael@0: i++; michael@0: } michael@0: if (start >= stop) { michael@0: throw self.StopIteration; michael@0: } michael@0: start += step; michael@0: return rval; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.imap */ michael@0: imap: function (fun, p, q/*, ...*/) { michael@0: var m = MochiKit.Base; michael@0: var self = MochiKit.Iter; michael@0: var iterables = m.map(self.iter, m.extend(null, arguments, 1)); michael@0: var map = m.map; michael@0: var next = self.next; michael@0: return { michael@0: repr: function () { return "imap(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: return fun.apply(this, map(next, iterables)); michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.applymap */ michael@0: applymap: function (fun, seq, self) { michael@0: seq = MochiKit.Iter.iter(seq); michael@0: var m = MochiKit.Base; michael@0: return { michael@0: repr: function () { return "applymap(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: return fun.apply(self, seq.next()); michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.chain */ michael@0: chain: function (p, q/*, ...*/) { michael@0: // dumb fast path michael@0: var self = MochiKit.Iter; michael@0: var m = MochiKit.Base; michael@0: if (arguments.length == 1) { michael@0: return self.iter(arguments[0]); michael@0: } michael@0: var argiter = m.map(self.iter, arguments); michael@0: return { michael@0: repr: function () { return "chain(...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: while (argiter.length > 1) { michael@0: try { michael@0: var result = argiter[0].next(); michael@0: return result; michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: argiter.shift(); michael@0: var result = argiter[0].next(); michael@0: return result; michael@0: } michael@0: } michael@0: if (argiter.length == 1) { michael@0: // optimize last element michael@0: var arg = argiter.shift(); michael@0: this.next = m.bind("next", arg); michael@0: return this.next(); michael@0: } michael@0: throw self.StopIteration; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.takewhile */ michael@0: takewhile: function (pred, seq) { michael@0: var self = MochiKit.Iter; michael@0: seq = self.iter(seq); michael@0: return { michael@0: repr: function () { return "takewhile(...)"; }, michael@0: toString: MochiKit.Base.forwardCall("repr"), michael@0: next: function () { michael@0: var rval = seq.next(); michael@0: if (!pred(rval)) { michael@0: this.next = function () { michael@0: throw self.StopIteration; michael@0: }; michael@0: this.next(); michael@0: } michael@0: return rval; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.dropwhile */ michael@0: dropwhile: function (pred, seq) { michael@0: seq = MochiKit.Iter.iter(seq); michael@0: var m = MochiKit.Base; michael@0: var bind = m.bind; michael@0: return { michael@0: "repr": function () { return "dropwhile(...)"; }, michael@0: "toString": m.forwardCall("repr"), michael@0: "next": function () { michael@0: while (true) { michael@0: var rval = seq.next(); michael@0: if (!pred(rval)) { michael@0: break; michael@0: } michael@0: } michael@0: this.next = bind("next", seq); michael@0: return rval; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: _tee: function (ident, sync, iterable) { michael@0: sync.pos[ident] = -1; michael@0: var m = MochiKit.Base; michael@0: var listMin = m.listMin; michael@0: return { michael@0: repr: function () { return "tee(" + ident + ", ...)"; }, michael@0: toString: m.forwardCall("repr"), michael@0: next: function () { michael@0: var rval; michael@0: var i = sync.pos[ident]; michael@0: michael@0: if (i == sync.max) { michael@0: rval = iterable.next(); michael@0: sync.deque.push(rval); michael@0: sync.max += 1; michael@0: sync.pos[ident] += 1; michael@0: } else { michael@0: rval = sync.deque[i - sync.min]; michael@0: sync.pos[ident] += 1; michael@0: if (i == sync.min && listMin(sync.pos) != sync.min) { michael@0: sync.min += 1; michael@0: sync.deque.shift(); michael@0: } michael@0: } michael@0: return rval; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.tee */ michael@0: tee: function (iterable, n/* = 2 */) { michael@0: var rval = []; michael@0: var sync = { michael@0: "pos": [], michael@0: "deque": [], michael@0: "max": -1, michael@0: "min": -1 michael@0: }; michael@0: if (arguments.length == 1 || typeof(n) == "undefined" || n === null) { michael@0: n = 2; michael@0: } michael@0: var self = MochiKit.Iter; michael@0: iterable = self.iter(iterable); michael@0: var _tee = self._tee; michael@0: for (var i = 0; i < n; i++) { michael@0: rval.push(_tee(i, sync, iterable)); michael@0: } michael@0: return rval; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.list */ michael@0: list: function (iterable) { michael@0: // Fast-path for Array and Array-like michael@0: var rval; michael@0: if (iterable instanceof Array) { michael@0: return iterable.slice(); michael@0: } michael@0: // this is necessary to avoid a Safari crash michael@0: if (typeof(iterable) == "function" && michael@0: !(iterable instanceof Function) && michael@0: typeof(iterable.length) == 'number') { michael@0: rval = []; michael@0: for (var i = 0; i < iterable.length; i++) { michael@0: rval.push(iterable[i]); michael@0: } michael@0: return rval; michael@0: } michael@0: michael@0: var self = MochiKit.Iter; michael@0: iterable = self.iter(iterable); michael@0: var rval = []; michael@0: var a_val; michael@0: try { michael@0: while (true) { michael@0: a_val = iterable.next(); michael@0: rval.push(a_val); michael@0: } michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: return rval; michael@0: } michael@0: // mozilla warnings aren't too bright michael@0: return undefined; michael@0: }, michael@0: michael@0: michael@0: /** @id MochiKit.Iter.reduce */ michael@0: reduce: function (fn, iterable, /* optional */initial) { michael@0: var i = 0; michael@0: var x = initial; michael@0: var self = MochiKit.Iter; michael@0: iterable = self.iter(iterable); michael@0: if (arguments.length < 3) { michael@0: try { michael@0: x = iterable.next(); michael@0: } catch (e) { michael@0: if (e == self.StopIteration) { michael@0: e = new TypeError("reduce() of empty sequence with no initial value"); michael@0: } michael@0: throw e; michael@0: } michael@0: i++; michael@0: } michael@0: try { michael@0: while (true) { michael@0: x = fn(x, iterable.next()); michael@0: } michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: } michael@0: return x; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.range */ michael@0: range: function (/* [start,] stop[, step] */) { michael@0: var start = 0; michael@0: var stop = 0; michael@0: var step = 1; michael@0: if (arguments.length == 1) { michael@0: stop = arguments[0]; michael@0: } else if (arguments.length == 2) { michael@0: start = arguments[0]; michael@0: stop = arguments[1]; michael@0: } else if (arguments.length == 3) { michael@0: start = arguments[0]; michael@0: stop = arguments[1]; michael@0: step = arguments[2]; michael@0: } else { michael@0: throw new TypeError("range() takes 1, 2, or 3 arguments!"); michael@0: } michael@0: if (step === 0) { michael@0: throw new TypeError("range() step must not be 0"); michael@0: } michael@0: return { michael@0: next: function () { michael@0: if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { michael@0: throw MochiKit.Iter.StopIteration; michael@0: } michael@0: var rval = start; michael@0: start += step; michael@0: return rval; michael@0: }, michael@0: repr: function () { michael@0: return "range(" + [start, stop, step].join(", ") + ")"; michael@0: }, michael@0: toString: MochiKit.Base.forwardCall("repr") michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.sum */ michael@0: sum: function (iterable, start/* = 0 */) { michael@0: if (typeof(start) == "undefined" || start === null) { michael@0: start = 0; michael@0: } michael@0: var x = start; michael@0: var self = MochiKit.Iter; michael@0: iterable = self.iter(iterable); michael@0: try { michael@0: while (true) { michael@0: x += iterable.next(); michael@0: } michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: } michael@0: return x; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.exhaust */ michael@0: exhaust: function (iterable) { michael@0: var self = MochiKit.Iter; michael@0: iterable = self.iter(iterable); michael@0: try { michael@0: while (true) { michael@0: iterable.next(); michael@0: } michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.forEach */ michael@0: forEach: function (iterable, func, /* optional */obj) { michael@0: var m = MochiKit.Base; michael@0: var self = MochiKit.Iter; michael@0: if (arguments.length > 2) { michael@0: func = m.bind(func, obj); michael@0: } michael@0: // fast path for array michael@0: if (m.isArrayLike(iterable) && !self.isIterable(iterable)) { michael@0: try { michael@0: for (var i = 0; i < iterable.length; i++) { michael@0: func(iterable[i]); michael@0: } michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: } michael@0: } else { michael@0: self.exhaust(self.imap(func, iterable)); michael@0: } michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.every */ michael@0: every: function (iterable, func) { michael@0: var self = MochiKit.Iter; michael@0: try { michael@0: self.ifilterfalse(func, iterable).next(); michael@0: return false; michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: return true; michael@0: } michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.sorted */ michael@0: sorted: function (iterable, /* optional */cmp) { michael@0: var rval = MochiKit.Iter.list(iterable); michael@0: if (arguments.length == 1) { michael@0: cmp = MochiKit.Base.compare; michael@0: } michael@0: rval.sort(cmp); michael@0: return rval; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.reversed */ michael@0: reversed: function (iterable) { michael@0: var rval = MochiKit.Iter.list(iterable); michael@0: rval.reverse(); michael@0: return rval; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.some */ michael@0: some: function (iterable, func) { michael@0: var self = MochiKit.Iter; michael@0: try { michael@0: self.ifilter(func, iterable).next(); michael@0: return true; michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: return false; michael@0: } michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.iextend */ michael@0: iextend: function (lst, iterable) { michael@0: var m = MochiKit.Base; michael@0: var self = MochiKit.Iter; michael@0: if (m.isArrayLike(iterable) && !self.isIterable(iterable)) { michael@0: // fast-path for array-like michael@0: for (var i = 0; i < iterable.length; i++) { michael@0: lst.push(iterable[i]); michael@0: } michael@0: } else { michael@0: iterable = self.iter(iterable); michael@0: try { michael@0: while (true) { michael@0: lst.push(iterable.next()); michael@0: } michael@0: } catch (e) { michael@0: if (e != self.StopIteration) { michael@0: throw e; michael@0: } michael@0: } michael@0: } michael@0: return lst; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.groupby */ michael@0: groupby: function(iterable, /* optional */ keyfunc) { michael@0: var m = MochiKit.Base; michael@0: var self = MochiKit.Iter; michael@0: if (arguments.length < 2) { michael@0: keyfunc = m.operator.identity; michael@0: } michael@0: iterable = self.iter(iterable); michael@0: michael@0: // shared michael@0: var pk = undefined; michael@0: var k = undefined; michael@0: var v; michael@0: michael@0: function fetch() { michael@0: v = iterable.next(); michael@0: k = keyfunc(v); michael@0: }; michael@0: michael@0: function eat() { michael@0: var ret = v; michael@0: v = undefined; michael@0: return ret; michael@0: }; michael@0: michael@0: var first = true; michael@0: var compare = m.compare; michael@0: return { michael@0: repr: function () { return "groupby(...)"; }, michael@0: next: function() { michael@0: // iterator-next michael@0: michael@0: // iterate until meet next group michael@0: while (compare(k, pk) === 0) { michael@0: fetch(); michael@0: if (first) { michael@0: first = false; michael@0: break; michael@0: } michael@0: } michael@0: pk = k; michael@0: return [k, { michael@0: next: function() { michael@0: // subiterator-next michael@0: if (v == undefined) { // Is there something to eat? michael@0: fetch(); michael@0: } michael@0: if (compare(k, pk) !== 0) { michael@0: throw self.StopIteration; michael@0: } michael@0: return eat(); michael@0: } michael@0: }]; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.groupby_as_array */ michael@0: groupby_as_array: function (iterable, /* optional */ keyfunc) { michael@0: var m = MochiKit.Base; michael@0: var self = MochiKit.Iter; michael@0: if (arguments.length < 2) { michael@0: keyfunc = m.operator.identity; michael@0: } michael@0: michael@0: iterable = self.iter(iterable); michael@0: var result = []; michael@0: var first = true; michael@0: var prev_key; michael@0: var compare = m.compare; michael@0: while (true) { michael@0: try { michael@0: var value = iterable.next(); michael@0: var key = keyfunc(value); michael@0: } catch (e) { michael@0: if (e == self.StopIteration) { michael@0: break; michael@0: } michael@0: throw e; michael@0: } michael@0: if (first || compare(key, prev_key) !== 0) { michael@0: var values = []; michael@0: result.push([key, values]); michael@0: } michael@0: values.push(value); michael@0: first = false; michael@0: prev_key = key; michael@0: } michael@0: return result; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.arrayLikeIter */ michael@0: arrayLikeIter: function (iterable) { michael@0: var i = 0; michael@0: return { michael@0: repr: function () { return "arrayLikeIter(...)"; }, michael@0: toString: MochiKit.Base.forwardCall("repr"), michael@0: next: function () { michael@0: if (i >= iterable.length) { michael@0: throw MochiKit.Iter.StopIteration; michael@0: } michael@0: return iterable[i++]; michael@0: } michael@0: }; michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.hasIterateNext */ michael@0: hasIterateNext: function (iterable) { michael@0: return (iterable && typeof(iterable.iterateNext) == "function"); michael@0: }, michael@0: michael@0: /** @id MochiKit.Iter.iterateNextIter */ michael@0: iterateNextIter: function (iterable) { michael@0: return { michael@0: repr: function () { return "iterateNextIter(...)"; }, michael@0: toString: MochiKit.Base.forwardCall("repr"), michael@0: next: function () { michael@0: var rval = iterable.iterateNext(); michael@0: if (rval === null || rval === undefined) { michael@0: throw MochiKit.Iter.StopIteration; michael@0: } michael@0: return rval; michael@0: } michael@0: }; michael@0: } michael@0: }); michael@0: michael@0: michael@0: MochiKit.Iter.EXPORT_OK = [ michael@0: "iteratorRegistry", michael@0: "arrayLikeIter", michael@0: "hasIterateNext", michael@0: "iterateNextIter" michael@0: ]; michael@0: michael@0: MochiKit.Iter.EXPORT = [ michael@0: "StopIteration", michael@0: "registerIteratorFactory", michael@0: "iter", michael@0: "count", michael@0: "cycle", michael@0: "repeat", michael@0: "next", michael@0: "izip", michael@0: "ifilter", michael@0: "ifilterfalse", michael@0: "islice", michael@0: "imap", michael@0: "applymap", michael@0: "chain", michael@0: "takewhile", michael@0: "dropwhile", michael@0: "tee", michael@0: "list", michael@0: "reduce", michael@0: "range", michael@0: "sum", michael@0: "exhaust", michael@0: "forEach", michael@0: "every", michael@0: "sorted", michael@0: "reversed", michael@0: "some", michael@0: "iextend", michael@0: "groupby", michael@0: "groupby_as_array" michael@0: ]; michael@0: michael@0: MochiKit.Iter.__new__ = function () { michael@0: var m = MochiKit.Base; michael@0: // Re-use StopIteration if exists (e.g. SpiderMonkey) michael@0: if (typeof(StopIteration) != "undefined") { michael@0: this.StopIteration = StopIteration; michael@0: } else { michael@0: /** @id MochiKit.Iter.StopIteration */ michael@0: this.StopIteration = new m.NamedError("StopIteration"); michael@0: } michael@0: this.iteratorRegistry = new m.AdapterRegistry(); michael@0: // Register the iterator factory for arrays michael@0: this.registerIteratorFactory( michael@0: "arrayLike", michael@0: m.isArrayLike, michael@0: this.arrayLikeIter michael@0: ); michael@0: michael@0: this.registerIteratorFactory( michael@0: "iterateNext", michael@0: this.hasIterateNext, michael@0: this.iterateNextIter michael@0: ); michael@0: michael@0: this.EXPORT_TAGS = { michael@0: ":common": this.EXPORT, michael@0: ":all": m.concat(this.EXPORT, this.EXPORT_OK) michael@0: }; michael@0: michael@0: m.nameFunctions(this); michael@0: michael@0: }; michael@0: michael@0: MochiKit.Iter.__new__(); michael@0: michael@0: // michael@0: // XXX: Internet Explorer blows michael@0: // michael@0: if (MochiKit.__export__) { michael@0: reduce = MochiKit.Iter.reduce; michael@0: } michael@0: michael@0: MochiKit.Base._exportSymbols(this, MochiKit.Iter);