Diffstat (limited to 'frontend/gamma/js/MochiKit/Iter.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/MochiKit/Iter.js | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/frontend/gamma/js/MochiKit/Iter.js b/frontend/gamma/js/MochiKit/Iter.js index 524b2bc..77623bc 100644 --- a/frontend/gamma/js/MochiKit/Iter.js +++ b/frontend/gamma/js/MochiKit/Iter.js | |||
@@ -1,608 +1,606 @@ | |||
1 | /*** | 1 | /*** |
2 | 2 | ||
3 | MochiKit.Iter 1.5 | 3 | MochiKit.Iter 1.5 |
4 | 4 | ||
5 | See <http://mochikit.com/> for documentation, downloads, license, etc. | 5 | See <http://mochikit.com/> for documentation, downloads, license, etc. |
6 | 6 | ||
7 | (c) 2005 Bob Ippolito. All rights Reserved. | 7 | (c) 2005 Bob Ippolito. All rights Reserved. |
8 | 8 | ||
9 | ***/ | 9 | ***/ |
10 | 10 | ||
11 | MochiKit.Base._module('Iter', '1.5', ['Base']); | 11 | MochiKit.Base.module(MochiKit, 'Iter', '1.5', ['Base']); |
12 | 12 | ||
13 | MochiKit.Base.update(MochiKit.Iter, { | 13 | MochiKit.Base.update(MochiKit.Iter, { |
14 | /** @id MochiKit.Iter.registerIteratorFactory */ | 14 | /** @id MochiKit.Iter.registerIteratorFactory */ |
15 | registerIteratorFactory: function (name, check, iterfactory, /* optional */ override) { | 15 | registerIteratorFactory: function (name, check, iterfactory, /* optional */ override) { |
16 | MochiKit.Iter.iteratorRegistry.register(name, check, iterfactory, override); | 16 | MochiKit.Iter.iteratorRegistry.register(name, check, iterfactory, override); |
17 | }, | 17 | }, |
18 | 18 | ||
19 | /** @id MochiKit.Iter.isIterable */ | 19 | /** @id MochiKit.Iter.isIterable */ |
20 | isIterable: function(o) { | 20 | isIterable: function(o) { |
21 | return o != null && | 21 | return o != null && |
22 | (typeof(o.next) == "function" || typeof(o.iter) == "function"); | 22 | (typeof(o.next) == "function" || typeof(o.iter) == "function"); |
23 | }, | 23 | }, |
24 | 24 | ||
25 | /** @id MochiKit.Iter.iter */ | 25 | /** @id MochiKit.Iter.iter */ |
26 | iter: function (iterable, /* optional */ sentinel) { | 26 | iter: function (iterable, /* optional */ sentinel) { |
27 | var self = MochiKit.Iter; | 27 | var self = MochiKit.Iter; |
28 | if (arguments.length == 2) { | 28 | if (arguments.length == 2) { |
29 | return self.takewhile( | 29 | return self.takewhile( |
30 | function (a) { return a != sentinel; }, | 30 | function (a) { return a != sentinel; }, |
31 | iterable | 31 | iterable |
32 | ); | 32 | ); |
33 | } | 33 | } |
34 | if (typeof(iterable.next) == 'function') { | 34 | if (typeof(iterable.next) == 'function') { |
35 | return iterable; | 35 | return iterable; |
36 | } else if (typeof(iterable.iter) == 'function') { | 36 | } else if (typeof(iterable.iter) == 'function') { |
37 | return iterable.iter(); | 37 | return iterable.iter(); |
38 | /* | 38 | /* |
39 | } else if (typeof(iterable.__iterator__) == 'function') { | 39 | } else if (typeof(iterable.__iterator__) == 'function') { |
40 | // | 40 | // |
41 | // XXX: We can't support JavaScript 1.7 __iterator__ directly | 41 | // XXX: We can't support JavaScript 1.7 __iterator__ directly |
42 | // because of Object.prototype.__iterator__ | 42 | // because of Object.prototype.__iterator__ |
43 | // | 43 | // |
44 | return iterable.__iterator__(); | 44 | return iterable.__iterator__(); |
45 | */ | 45 | */ |
46 | } | 46 | } |
47 | 47 | ||
48 | try { | 48 | try { |
49 | return self.iteratorRegistry.match(iterable); | 49 | return self.iteratorRegistry.match(iterable); |
50 | } catch (e) { | 50 | } catch (e) { |
51 | var m = MochiKit.Base; | 51 | var m = MochiKit.Base; |
52 | if (e == m.NotFound) { | 52 | if (e == m.NotFound) { |
53 | e = new TypeError(typeof(iterable) + ": " + m.repr(iterable) + " is not iterable"); | 53 | e = new TypeError(typeof(iterable) + ": " + m.repr(iterable) + " is not iterable"); |
54 | } | 54 | } |
55 | throw e; | 55 | throw e; |
56 | } | 56 | } |
57 | }, | 57 | }, |
58 | 58 | ||
59 | /** @id MochiKit.Iter.count */ | 59 | /** @id MochiKit.Iter.count */ |
60 | count: function (n) { | 60 | count: function (n) { |
61 | if (!n) { | 61 | if (!n) { |
62 | n = 0; | 62 | n = 0; |
63 | } | 63 | } |
64 | var m = MochiKit.Base; | 64 | var m = MochiKit.Base; |
65 | return { | 65 | return { |
66 | repr: function () { return "count(" + n + ")"; }, | 66 | repr: function () { return "count(" + n + ")"; }, |
67 | toString: m.forwardCall("repr"), | 67 | toString: m.forwardCall("repr"), |
68 | next: m.counter(n) | 68 | next: m.counter(n) |
69 | }; | 69 | }; |
70 | }, | 70 | }, |
71 | 71 | ||
72 | /** @id MochiKit.Iter.cycle */ | 72 | /** @id MochiKit.Iter.cycle */ |
73 | cycle: function (p) { | 73 | cycle: function (p) { |
74 | var self = MochiKit.Iter; | 74 | var self = MochiKit.Iter; |
75 | var m = MochiKit.Base; | 75 | var m = MochiKit.Base; |
76 | var lst = []; | 76 | var lst = []; |
77 | var iterator = self.iter(p); | 77 | var iterator = self.iter(p); |
78 | return { | 78 | return { |
79 | repr: function () { return "cycle(...)"; }, | 79 | repr: function () { return "cycle(...)"; }, |
80 | toString: m.forwardCall("repr"), | 80 | toString: m.forwardCall("repr"), |
81 | next: function () { | 81 | next: function () { |
82 | try { | 82 | try { |
83 | var rval = iterator.next(); | 83 | var rval = iterator.next(); |
84 | lst.push(rval); | 84 | lst.push(rval); |
85 | return rval; | 85 | return rval; |
86 | } catch (e) { | 86 | } catch (e) { |
87 | if (e != self.StopIteration) { | 87 | if (e != self.StopIteration) { |
88 | throw e; | 88 | throw e; |
89 | } | 89 | } |
90 | if (lst.length === 0) { | 90 | if (lst.length === 0) { |
91 | this.next = function () { | 91 | this.next = function () { |
92 | throw self.StopIteration; | 92 | throw self.StopIteration; |
93 | }; | 93 | }; |
94 | } else { | 94 | } else { |
95 | var i = -1; | 95 | var i = -1; |
96 | this.next = function () { | 96 | this.next = function () { |
97 | i = (i + 1) % lst.length; | 97 | i = (i + 1) % lst.length; |
98 | return lst[i]; | 98 | return lst[i]; |
99 | }; | 99 | }; |
100 | } | 100 | } |
101 | return this.next(); | 101 | return this.next(); |
102 | } | 102 | } |
103 | } | 103 | } |
104 | }; | 104 | }; |
105 | }, | 105 | }, |
106 | 106 | ||
107 | /** @id MochiKit.Iter.repeat */ | 107 | /** @id MochiKit.Iter.repeat */ |
108 | repeat: function (elem, /* optional */n) { | 108 | repeat: function (elem, /* optional */n) { |
109 | var m = MochiKit.Base; | 109 | var m = MochiKit.Base; |
110 | if (typeof(n) == 'undefined') { | 110 | if (typeof(n) == 'undefined') { |
111 | return { | 111 | return { |
112 | repr: function () { | 112 | repr: function () { |
113 | return "repeat(" + m.repr(elem) + ")"; | 113 | return "repeat(" + m.repr(elem) + ")"; |
114 | }, | 114 | }, |
115 | toString: m.forwardCall("repr"), | 115 | toString: m.forwardCall("repr"), |
116 | next: function () { | 116 | next: function () { |
117 | return elem; | 117 | return elem; |
118 | } | 118 | } |
119 | }; | 119 | }; |
120 | } | 120 | } |
121 | return { | 121 | return { |
122 | repr: function () { | 122 | repr: function () { |
123 | return "repeat(" + m.repr(elem) + ", " + n + ")"; | 123 | return "repeat(" + m.repr(elem) + ", " + n + ")"; |
124 | }, | 124 | }, |
125 | toString: m.forwardCall("repr"), | 125 | toString: m.forwardCall("repr"), |
126 | next: function () { | 126 | next: function () { |
127 | if (n <= 0) { | 127 | if (n <= 0) { |
128 | throw MochiKit.Iter.StopIteration; | 128 | throw MochiKit.Iter.StopIteration; |
129 | } | 129 | } |
130 | n -= 1; | 130 | n -= 1; |
131 | return elem; | 131 | return elem; |
132 | } | 132 | } |
133 | }; | 133 | }; |
134 | }, | 134 | }, |
135 | 135 | ||
136 | /** @id MochiKit.Iter.next */ | 136 | /** @id MochiKit.Iter.next */ |
137 | next: function (iterator) { | 137 | next: function (iterator) { |
138 | return iterator.next(); | 138 | return iterator.next(); |
139 | }, | 139 | }, |
140 | 140 | ||
141 | /** @id MochiKit.Iter.izip */ | 141 | /** @id MochiKit.Iter.izip */ |
142 | izip: function (p, q/*, ...*/) { | 142 | izip: function (p, q/*, ...*/) { |
143 | var m = MochiKit.Base; | 143 | var m = MochiKit.Base; |
144 | var self = MochiKit.Iter; | 144 | var self = MochiKit.Iter; |
145 | var next = self.next; | 145 | var next = self.next; |
146 | var iterables = m.map(self.iter, arguments); | 146 | var iterables = m.map(self.iter, arguments); |
147 | return { | 147 | return { |
148 | repr: function () { return "izip(...)"; }, | 148 | repr: function () { return "izip(...)"; }, |
149 | toString: m.forwardCall("repr"), | 149 | toString: m.forwardCall("repr"), |
150 | next: function () { return m.map(next, iterables); } | 150 | next: function () { return m.map(next, iterables); } |
151 | }; | 151 | }; |
152 | }, | 152 | }, |
153 | 153 | ||
154 | /** @id MochiKit.Iter.ifilter */ | 154 | /** @id MochiKit.Iter.ifilter */ |
155 | ifilter: function (pred, seq) { | 155 | ifilter: function (pred, seq) { |
156 | var m = MochiKit.Base; | 156 | var m = MochiKit.Base; |
157 | seq = MochiKit.Iter.iter(seq); | 157 | seq = MochiKit.Iter.iter(seq); |
158 | if (pred === null) { | 158 | if (pred === null) { |
159 | pred = m.operator.truth; | 159 | pred = m.operator.truth; |
160 | } | 160 | } |
161 | return { | 161 | return { |
162 | repr: function () { return "ifilter(...)"; }, | 162 | repr: function () { return "ifilter(...)"; }, |
163 | toString: m.forwardCall("repr"), | 163 | toString: m.forwardCall("repr"), |
164 | next: function () { | 164 | next: function () { |
165 | while (true) { | 165 | while (true) { |
166 | var rval = seq.next(); | 166 | var rval = seq.next(); |
167 | if (pred(rval)) { | 167 | if (pred(rval)) { |
168 | return rval; | 168 | return rval; |
169 | } | 169 | } |
170 | } | 170 | } |
171 | // mozilla warnings aren't too bright | 171 | // mozilla warnings aren't too bright |
172 | return undefined; | 172 | return undefined; |
173 | } | 173 | } |
174 | }; | 174 | }; |
175 | }, | 175 | }, |
176 | 176 | ||
177 | /** @id MochiKit.Iter.ifilterfalse */ | 177 | /** @id MochiKit.Iter.ifilterfalse */ |
178 | ifilterfalse: function (pred, seq) { | 178 | ifilterfalse: function (pred, seq) { |
179 | var m = MochiKit.Base; | 179 | var m = MochiKit.Base; |
180 | seq = MochiKit.Iter.iter(seq); | 180 | seq = MochiKit.Iter.iter(seq); |
181 | if (pred === null) { | 181 | if (pred === null) { |
182 | pred = m.operator.truth; | 182 | pred = m.operator.truth; |
183 | } | 183 | } |
184 | return { | 184 | return { |
185 | repr: function () { return "ifilterfalse(...)"; }, | 185 | repr: function () { return "ifilterfalse(...)"; }, |
186 | toString: m.forwardCall("repr"), | 186 | toString: m.forwardCall("repr"), |
187 | next: function () { | 187 | next: function () { |
188 | while (true) { | 188 | while (true) { |
189 | var rval = seq.next(); | 189 | var rval = seq.next(); |
190 | if (!pred(rval)) { | 190 | if (!pred(rval)) { |
191 | return rval; | 191 | return rval; |
192 | } | 192 | } |
193 | } | 193 | } |
194 | // mozilla warnings aren't too bright | 194 | // mozilla warnings aren't too bright |
195 | return undefined; | 195 | return undefined; |
196 | } | 196 | } |
197 | }; | 197 | }; |
198 | }, | 198 | }, |
199 | 199 | ||
200 | /** @id MochiKit.Iter.islice */ | 200 | /** @id MochiKit.Iter.islice */ |
201 | islice: function (seq/*, [start,] stop[, step] */) { | 201 | islice: function (seq/*, [start,] stop[, step] */) { |
202 | var self = MochiKit.Iter; | 202 | var self = MochiKit.Iter; |
203 | var m = MochiKit.Base; | 203 | var m = MochiKit.Base; |
204 | seq = self.iter(seq); | 204 | seq = self.iter(seq); |
205 | var start = 0; | 205 | var start = 0; |
206 | var stop = 0; | 206 | var stop = 0; |
207 | var step = 1; | 207 | var step = 1; |
208 | var i = -1; | 208 | var i = -1; |
209 | if (arguments.length == 2) { | 209 | if (arguments.length == 2) { |
210 | stop = arguments[1]; | 210 | stop = arguments[1]; |
211 | } else if (arguments.length == 3) { | 211 | } else if (arguments.length == 3) { |
212 | start = arguments[1]; | 212 | start = arguments[1]; |
213 | stop = arguments[2]; | 213 | stop = arguments[2]; |
214 | } else { | 214 | } else { |
215 | start = arguments[1]; | 215 | start = arguments[1]; |
216 | stop = arguments[2]; | 216 | stop = arguments[2]; |
217 | step = arguments[3]; | 217 | step = arguments[3]; |
218 | } | 218 | } |
219 | return { | 219 | return { |
220 | repr: function () { | 220 | repr: function () { |
221 | return "islice(" + ["...", start, stop, step].join(", ") + ")"; | 221 | return "islice(" + ["...", start, stop, step].join(", ") + ")"; |
222 | }, | 222 | }, |
223 | toString: m.forwardCall("repr"), | 223 | toString: m.forwardCall("repr"), |
224 | next: function () { | 224 | next: function () { |
225 | if (start >= stop) { | ||
226 | throw self.StopIteration; | ||
227 | } | ||
228 | |||
225 | var rval; | 229 | var rval; |
226 | while (i < start) { | 230 | while (i < start) { |
227 | rval = seq.next(); | 231 | rval = seq.next(); |
228 | i++; | 232 | i++; |
229 | } | 233 | } |
230 | if (start >= stop) { | ||
231 | throw self.StopIteration; | ||
232 | } | ||
233 | start += step; | 234 | start += step; |
234 | return rval; | 235 | return rval; |
235 | } | 236 | } |
236 | }; | 237 | }; |
237 | }, | 238 | }, |
238 | 239 | ||
239 | /** @id MochiKit.Iter.imap */ | 240 | /** @id MochiKit.Iter.imap */ |
240 | imap: function (fun, p, q/*, ...*/) { | 241 | imap: function (fun, p, q/*, ...*/) { |
241 | var m = MochiKit.Base; | 242 | var m = MochiKit.Base; |
242 | var self = MochiKit.Iter; | 243 | var self = MochiKit.Iter; |
243 | var iterables = m.map(self.iter, m.extend(null, arguments, 1)); | 244 | var iterables = m.map(self.iter, m.extend(null, arguments, 1)); |
244 | var map = m.map; | 245 | var map = m.map; |
245 | var next = self.next; | 246 | var next = self.next; |
246 | return { | 247 | return { |
247 | repr: function () { return "imap(...)"; }, | 248 | repr: function () { return "imap(...)"; }, |
248 | toString: m.forwardCall("repr"), | 249 | toString: m.forwardCall("repr"), |
249 | next: function () { | 250 | next: function () { |
250 | return fun.apply(this, map(next, iterables)); | 251 | return fun.apply(this, map(next, iterables)); |
251 | } | 252 | } |
252 | }; | 253 | }; |
253 | }, | 254 | }, |
254 | 255 | ||
255 | /** @id MochiKit.Iter.applymap */ | 256 | /** @id MochiKit.Iter.applymap */ |
256 | applymap: function (fun, seq, self) { | 257 | applymap: function (fun, seq, self) { |
257 | seq = MochiKit.Iter.iter(seq); | 258 | seq = MochiKit.Iter.iter(seq); |
258 | var m = MochiKit.Base; | 259 | var m = MochiKit.Base; |
259 | return { | 260 | return { |
260 | repr: function () { return "applymap(...)"; }, | 261 | repr: function () { return "applymap(...)"; }, |
261 | toString: m.forwardCall("repr"), | 262 | toString: m.forwardCall("repr"), |
262 | next: function () { | 263 | next: function () { |
263 | return fun.apply(self, seq.next()); | 264 | return fun.apply(self, seq.next()); |
264 | } | 265 | } |
265 | }; | 266 | }; |
266 | }, | 267 | }, |
267 | 268 | ||
268 | /** @id MochiKit.Iter.chain */ | 269 | /** @id MochiKit.Iter.chain */ |
269 | chain: function (p, q/*, ...*/) { | 270 | chain: function (p, q/*, ...*/) { |
270 | // dumb fast path | 271 | // dumb fast path |
271 | var self = MochiKit.Iter; | 272 | var self = MochiKit.Iter; |
272 | var m = MochiKit.Base; | 273 | var m = MochiKit.Base; |
273 | if (arguments.length == 1) { | 274 | if (arguments.length == 1) { |
274 | return self.iter(arguments[0]); | 275 | return self.iter(arguments[0]); |
275 | } | 276 | } |
276 | var argiter = m.map(self.iter, arguments); | 277 | var argiter = m.map(self.iter, arguments); |
277 | return { | 278 | return { |
278 | repr: function () { return "chain(...)"; }, | 279 | repr: function () { return "chain(...)"; }, |
279 | toString: m.forwardCall("repr"), | 280 | toString: m.forwardCall("repr"), |
280 | next: function () { | 281 | next: function () { |
281 | while (argiter.length > 1) { | 282 | while (argiter.length > 1) { |
282 | try { | 283 | try { |
283 | var result = argiter[0].next(); | 284 | return argiter[0].next(); |
284 | return result; | ||
285 | } catch (e) { | 285 | } catch (e) { |
286 | if (e != self.StopIteration) { | 286 | if (e != self.StopIteration) { |
287 | throw e; | 287 | throw e; |
288 | } | 288 | } |
289 | argiter.shift(); | 289 | argiter.shift(); |
290 | var result = argiter[0].next(); | ||
291 | return result; | ||
292 | } | 290 | } |
293 | } | 291 | } |
294 | if (argiter.length == 1) { | 292 | if (argiter.length == 1) { |
295 | // optimize last element | 293 | // optimize last element |
296 | var arg = argiter.shift(); | 294 | var arg = argiter.shift(); |
297 | this.next = m.bind("next", arg); | 295 | this.next = m.bind("next", arg); |
298 | return this.next(); | 296 | return this.next(); |
299 | } | 297 | } |
300 | throw self.StopIteration; | 298 | throw self.StopIteration; |
301 | } | 299 | } |
302 | }; | 300 | }; |
303 | }, | 301 | }, |
304 | 302 | ||
305 | /** @id MochiKit.Iter.takewhile */ | 303 | /** @id MochiKit.Iter.takewhile */ |
306 | takewhile: function (pred, seq) { | 304 | takewhile: function (pred, seq) { |
307 | var self = MochiKit.Iter; | 305 | var self = MochiKit.Iter; |
308 | seq = self.iter(seq); | 306 | seq = self.iter(seq); |
309 | return { | 307 | return { |
310 | repr: function () { return "takewhile(...)"; }, | 308 | repr: function () { return "takewhile(...)"; }, |
311 | toString: MochiKit.Base.forwardCall("repr"), | 309 | toString: MochiKit.Base.forwardCall("repr"), |
312 | next: function () { | 310 | next: function () { |
313 | var rval = seq.next(); | 311 | var rval = seq.next(); |
314 | if (!pred(rval)) { | 312 | if (!pred(rval)) { |
315 | this.next = function () { | 313 | this.next = function () { |
316 | throw self.StopIteration; | 314 | throw self.StopIteration; |
317 | }; | 315 | }; |
318 | this.next(); | 316 | this.next(); |
319 | } | 317 | } |
320 | return rval; | 318 | return rval; |
321 | } | 319 | } |
322 | }; | 320 | }; |
323 | }, | 321 | }, |
324 | 322 | ||
325 | /** @id MochiKit.Iter.dropwhile */ | 323 | /** @id MochiKit.Iter.dropwhile */ |
326 | dropwhile: function (pred, seq) { | 324 | dropwhile: function (pred, seq) { |
327 | seq = MochiKit.Iter.iter(seq); | 325 | seq = MochiKit.Iter.iter(seq); |
328 | var m = MochiKit.Base; | 326 | var m = MochiKit.Base; |
329 | var bind = m.bind; | 327 | var bind = m.bind; |
330 | return { | 328 | return { |
331 | "repr": function () { return "dropwhile(...)"; }, | 329 | "repr": function () { return "dropwhile(...)"; }, |
332 | "toString": m.forwardCall("repr"), | 330 | "toString": m.forwardCall("repr"), |
333 | "next": function () { | 331 | "next": function () { |
334 | while (true) { | 332 | while (true) { |
335 | var rval = seq.next(); | 333 | var rval = seq.next(); |
336 | if (!pred(rval)) { | 334 | if (!pred(rval)) { |
337 | break; | 335 | break; |
338 | } | 336 | } |
339 | } | 337 | } |
340 | this.next = bind("next", seq); | 338 | this.next = bind("next", seq); |
341 | return rval; | 339 | return rval; |
342 | } | 340 | } |
343 | }; | 341 | }; |
344 | }, | 342 | }, |
345 | 343 | ||
346 | _tee: function (ident, sync, iterable) { | 344 | _tee: function (ident, sync, iterable) { |
347 | sync.pos[ident] = -1; | 345 | sync.pos[ident] = -1; |
348 | var m = MochiKit.Base; | 346 | var m = MochiKit.Base; |
349 | var listMin = m.listMin; | 347 | var listMin = m.listMin; |
350 | return { | 348 | return { |
351 | repr: function () { return "tee(" + ident + ", ...)"; }, | 349 | repr: function () { return "tee(" + ident + ", ...)"; }, |
352 | toString: m.forwardCall("repr"), | 350 | toString: m.forwardCall("repr"), |
353 | next: function () { | 351 | next: function () { |
354 | var rval; | 352 | var rval; |
355 | var i = sync.pos[ident]; | 353 | var i = sync.pos[ident]; |
356 | 354 | ||
357 | if (i == sync.max) { | 355 | if (i == sync.max) { |
358 | rval = iterable.next(); | 356 | rval = iterable.next(); |
359 | sync.deque.push(rval); | 357 | sync.deque.push(rval); |
360 | sync.max += 1; | 358 | sync.max += 1; |
361 | sync.pos[ident] += 1; | 359 | sync.pos[ident] += 1; |
362 | } else { | 360 | } else { |
363 | rval = sync.deque[i - sync.min]; | 361 | rval = sync.deque[i - sync.min]; |
364 | sync.pos[ident] += 1; | 362 | sync.pos[ident] += 1; |
365 | if (i == sync.min && listMin(sync.pos) != sync.min) { | 363 | if (i == sync.min && listMin(sync.pos) != sync.min) { |
366 | sync.min += 1; | 364 | sync.min += 1; |
367 | sync.deque.shift(); | 365 | sync.deque.shift(); |
368 | } | 366 | } |
369 | } | 367 | } |
370 | return rval; | 368 | return rval; |
371 | } | 369 | } |
372 | }; | 370 | }; |
373 | }, | 371 | }, |
374 | 372 | ||
375 | /** @id MochiKit.Iter.tee */ | 373 | /** @id MochiKit.Iter.tee */ |
376 | tee: function (iterable, n/* = 2 */) { | 374 | tee: function (iterable, n/* = 2 */) { |
377 | var rval = []; | 375 | var rval = []; |
378 | var sync = { | 376 | var sync = { |
379 | "pos": [], | 377 | "pos": [], |
380 | "deque": [], | 378 | "deque": [], |
381 | "max": -1, | 379 | "max": -1, |
382 | "min": -1 | 380 | "min": -1 |
383 | }; | 381 | }; |
384 | if (arguments.length == 1 || typeof(n) == "undefined" || n === null) { | 382 | if (arguments.length == 1 || typeof(n) == "undefined" || n === null) { |
385 | n = 2; | 383 | n = 2; |
386 | } | 384 | } |
387 | var self = MochiKit.Iter; | 385 | var self = MochiKit.Iter; |
388 | iterable = self.iter(iterable); | 386 | iterable = self.iter(iterable); |
389 | var _tee = self._tee; | 387 | var _tee = self._tee; |
390 | for (var i = 0; i < n; i++) { | 388 | for (var i = 0; i < n; i++) { |
391 | rval.push(_tee(i, sync, iterable)); | 389 | rval.push(_tee(i, sync, iterable)); |
392 | } | 390 | } |
393 | return rval; | 391 | return rval; |
394 | }, | 392 | }, |
395 | 393 | ||
396 | /** @id MochiKit.Iter.list */ | 394 | /** @id MochiKit.Iter.list */ |
397 | list: function (iterable) { | 395 | list: function (iterable) { |
398 | // Fast-path for Array and Array-like | 396 | // Fast-path for Array and Array-like |
399 | var rval; | 397 | var rval; |
400 | if (iterable instanceof Array) { | 398 | if (iterable instanceof Array) { |
401 | return iterable.slice(); | 399 | return iterable.slice(); |
402 | } | 400 | } |
403 | // this is necessary to avoid a Safari crash | 401 | // this is necessary to avoid a Safari crash |
404 | if (typeof(iterable) == "function" && | 402 | if (typeof(iterable) == "function" && |
405 | !(iterable instanceof Function) && | 403 | !(iterable instanceof Function) && |
406 | typeof(iterable.length) == 'number') { | 404 | typeof(iterable.length) == 'number') { |
407 | rval = []; | 405 | rval = []; |
408 | for (var i = 0; i < iterable.length; i++) { | 406 | for (var i = 0; i < iterable.length; i++) { |
409 | rval.push(iterable[i]); | 407 | rval.push(iterable[i]); |
410 | } | 408 | } |
411 | return rval; | 409 | return rval; |
412 | } | 410 | } |
413 | 411 | ||
414 | var self = MochiKit.Iter; | 412 | var self = MochiKit.Iter; |
415 | iterable = self.iter(iterable); | 413 | iterable = self.iter(iterable); |
416 | var rval = []; | 414 | rval = []; |
417 | var a_val; | 415 | var a_val; |
418 | try { | 416 | try { |
419 | while (true) { | 417 | while (true) { |
420 | a_val = iterable.next(); | 418 | a_val = iterable.next(); |
421 | rval.push(a_val); | 419 | rval.push(a_val); |
422 | } | 420 | } |
423 | } catch (e) { | 421 | } catch (e) { |
424 | if (e != self.StopIteration) { | 422 | if (e != self.StopIteration) { |
425 | throw e; | 423 | throw e; |
426 | } | 424 | } |
427 | return rval; | 425 | return rval; |
428 | } | 426 | } |
429 | // mozilla warnings aren't too bright | 427 | // mozilla warnings aren't too bright |
430 | return undefined; | 428 | return undefined; |
431 | }, | 429 | }, |
432 | 430 | ||
433 | 431 | ||
434 | /** @id MochiKit.Iter.reduce */ | 432 | /** @id MochiKit.Iter.reduce */ |
435 | reduce: function (fn, iterable, /* optional */initial) { | 433 | reduce: function (fn, iterable, /* optional */initial) { |
436 | var i = 0; | 434 | var i = 0; |
437 | var x = initial; | 435 | var x = initial; |
438 | var self = MochiKit.Iter; | 436 | var self = MochiKit.Iter; |
439 | iterable = self.iter(iterable); | 437 | iterable = self.iter(iterable); |
440 | if (arguments.length < 3) { | 438 | if (arguments.length < 3) { |
441 | try { | 439 | try { |
442 | x = iterable.next(); | 440 | x = iterable.next(); |
443 | } catch (e) { | 441 | } catch (e) { |
444 | if (e == self.StopIteration) { | 442 | if (e == self.StopIteration) { |
445 | e = new TypeError("reduce() of empty sequence with no initial value"); | 443 | e = new TypeError("reduce() of empty sequence with no initial value"); |
446 | } | 444 | } |
447 | throw e; | 445 | throw e; |
448 | } | 446 | } |
449 | i++; | 447 | i++; |
450 | } | 448 | } |
451 | try { | 449 | try { |
452 | while (true) { | 450 | while (true) { |
453 | x = fn(x, iterable.next()); | 451 | x = fn(x, iterable.next()); |
454 | } | 452 | } |
455 | } catch (e) { | 453 | } catch (e) { |
456 | if (e != self.StopIteration) { | 454 | if (e != self.StopIteration) { |
457 | throw e; | 455 | throw e; |
458 | } | 456 | } |
459 | } | 457 | } |
460 | return x; | 458 | return x; |
461 | }, | 459 | }, |
462 | 460 | ||
463 | /** @id MochiKit.Iter.range */ | 461 | /** @id MochiKit.Iter.range */ |
464 | range: function (/* [start,] stop[, step] */) { | 462 | range: function (/* [start,] stop[, step] */) { |
465 | var start = 0; | 463 | var start = 0; |
466 | var stop = 0; | 464 | var stop = 0; |
467 | var step = 1; | 465 | var step = 1; |
468 | if (arguments.length == 1) { | 466 | if (arguments.length == 1) { |
469 | stop = arguments[0]; | 467 | stop = arguments[0]; |
470 | } else if (arguments.length == 2) { | 468 | } else if (arguments.length == 2) { |
471 | start = arguments[0]; | 469 | start = arguments[0]; |
472 | stop = arguments[1]; | 470 | stop = arguments[1]; |
473 | } else if (arguments.length == 3) { | 471 | } else if (arguments.length == 3) { |
474 | start = arguments[0]; | 472 | start = arguments[0]; |
475 | stop = arguments[1]; | 473 | stop = arguments[1]; |
476 | step = arguments[2]; | 474 | step = arguments[2]; |
477 | } else { | 475 | } else { |
478 | throw new TypeError("range() takes 1, 2, or 3 arguments!"); | 476 | throw new TypeError("range() takes 1, 2, or 3 arguments!"); |
479 | } | 477 | } |
480 | if (step === 0) { | 478 | if (step === 0) { |
481 | throw new TypeError("range() step must not be 0"); | 479 | throw new TypeError("range() step must not be 0"); |
482 | } | 480 | } |
483 | return { | 481 | return { |
484 | next: function () { | 482 | next: function () { |
485 | if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { | 483 | if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { |
486 | throw MochiKit.Iter.StopIteration; | 484 | throw MochiKit.Iter.StopIteration; |
487 | } | 485 | } |
488 | var rval = start; | 486 | var rval = start; |
489 | start += step; | 487 | start += step; |
490 | return rval; | 488 | return rval; |
491 | }, | 489 | }, |
492 | repr: function () { | 490 | repr: function () { |
493 | return "range(" + [start, stop, step].join(", ") + ")"; | 491 | return "range(" + [start, stop, step].join(", ") + ")"; |
494 | }, | 492 | }, |
495 | toString: MochiKit.Base.forwardCall("repr") | 493 | toString: MochiKit.Base.forwardCall("repr") |
496 | }; | 494 | }; |
497 | }, | 495 | }, |
498 | 496 | ||
499 | /** @id MochiKit.Iter.sum */ | 497 | /** @id MochiKit.Iter.sum */ |
500 | sum: function (iterable, start/* = 0 */) { | 498 | sum: function (iterable, start/* = 0 */) { |
501 | if (typeof(start) == "undefined" || start === null) { | 499 | if (typeof(start) == "undefined" || start === null) { |
502 | start = 0; | 500 | start = 0; |
503 | } | 501 | } |
504 | var x = start; | 502 | var x = start; |
505 | var self = MochiKit.Iter; | 503 | var self = MochiKit.Iter; |
506 | iterable = self.iter(iterable); | 504 | iterable = self.iter(iterable); |
507 | try { | 505 | try { |
508 | while (true) { | 506 | while (true) { |
509 | x += iterable.next(); | 507 | x += iterable.next(); |
510 | } | 508 | } |
511 | } catch (e) { | 509 | } catch (e) { |
512 | if (e != self.StopIteration) { | 510 | if (e != self.StopIteration) { |
513 | throw e; | 511 | throw e; |
514 | } | 512 | } |
515 | } | 513 | } |
516 | return x; | 514 | return x; |
517 | }, | 515 | }, |
518 | 516 | ||
519 | /** @id MochiKit.Iter.exhaust */ | 517 | /** @id MochiKit.Iter.exhaust */ |
520 | exhaust: function (iterable) { | 518 | exhaust: function (iterable) { |
521 | var self = MochiKit.Iter; | 519 | var self = MochiKit.Iter; |
522 | iterable = self.iter(iterable); | 520 | iterable = self.iter(iterable); |
523 | try { | 521 | try { |
524 | while (true) { | 522 | while (true) { |
525 | iterable.next(); | 523 | iterable.next(); |
526 | } | 524 | } |
527 | } catch (e) { | 525 | } catch (e) { |
528 | if (e != self.StopIteration) { | 526 | if (e != self.StopIteration) { |
529 | throw e; | 527 | throw e; |
530 | } | 528 | } |
531 | } | 529 | } |
532 | }, | 530 | }, |
533 | 531 | ||
534 | /** @id MochiKit.Iter.forEach */ | 532 | /** @id MochiKit.Iter.forEach */ |
535 | forEach: function (iterable, func, /* optional */obj) { | 533 | forEach: function (iterable, func, /* optional */obj) { |
536 | var m = MochiKit.Base; | 534 | var m = MochiKit.Base; |
537 | var self = MochiKit.Iter; | 535 | var self = MochiKit.Iter; |
538 | if (arguments.length > 2) { | 536 | if (arguments.length > 2) { |
539 | func = m.bind(func, obj); | 537 | func = m.bind(func, obj); |
540 | } | 538 | } |
541 | // fast path for array | 539 | // fast path for array |
542 | if (m.isArrayLike(iterable) && !self.isIterable(iterable)) { | 540 | if (m.isArrayLike(iterable) && !self.isIterable(iterable)) { |
543 | try { | 541 | try { |
544 | for (var i = 0; i < iterable.length; i++) { | 542 | for (var i = 0; i < iterable.length; i++) { |
545 | func(iterable[i]); | 543 | func(iterable[i]); |
546 | } | 544 | } |
547 | } catch (e) { | 545 | } catch (e) { |
548 | if (e != self.StopIteration) { | 546 | if (e != self.StopIteration) { |
549 | throw e; | 547 | throw e; |
550 | } | 548 | } |
551 | } | 549 | } |
552 | } else { | 550 | } else { |
553 | self.exhaust(self.imap(func, iterable)); | 551 | self.exhaust(self.imap(func, iterable)); |
554 | } | 552 | } |
555 | }, | 553 | }, |
556 | 554 | ||
557 | /** @id MochiKit.Iter.every */ | 555 | /** @id MochiKit.Iter.every */ |
558 | every: function (iterable, func) { | 556 | every: function (iterable, func) { |
559 | var self = MochiKit.Iter; | 557 | var self = MochiKit.Iter; |
560 | try { | 558 | try { |
561 | self.ifilterfalse(func, iterable).next(); | 559 | self.ifilterfalse(func, iterable).next(); |
562 | return false; | 560 | return false; |
563 | } catch (e) { | 561 | } catch (e) { |
564 | if (e != self.StopIteration) { | 562 | if (e != self.StopIteration) { |
565 | throw e; | 563 | throw e; |
566 | } | 564 | } |
567 | return true; | 565 | return true; |
568 | } | 566 | } |
569 | }, | 567 | }, |
570 | 568 | ||
571 | /** @id MochiKit.Iter.sorted */ | 569 | /** @id MochiKit.Iter.sorted */ |
572 | sorted: function (iterable, /* optional */cmp) { | 570 | sorted: function (iterable, /* optional */cmp) { |
573 | var rval = MochiKit.Iter.list(iterable); | 571 | var rval = MochiKit.Iter.list(iterable); |
574 | if (arguments.length == 1) { | 572 | if (arguments.length == 1) { |
575 | cmp = MochiKit.Base.compare; | 573 | cmp = MochiKit.Base.compare; |
576 | } | 574 | } |
577 | rval.sort(cmp); | 575 | rval.sort(cmp); |
578 | return rval; | 576 | return rval; |
579 | }, | 577 | }, |
580 | 578 | ||
581 | /** @id MochiKit.Iter.reversed */ | 579 | /** @id MochiKit.Iter.reversed */ |
582 | reversed: function (iterable) { | 580 | reversed: function (iterable) { |
583 | var rval = MochiKit.Iter.list(iterable); | 581 | var rval = MochiKit.Iter.list(iterable); |
584 | rval.reverse(); | 582 | rval.reverse(); |
585 | return rval; | 583 | return rval; |
586 | }, | 584 | }, |
587 | 585 | ||
588 | /** @id MochiKit.Iter.some */ | 586 | /** @id MochiKit.Iter.some */ |
589 | some: function (iterable, func) { | 587 | some: function (iterable, func) { |
590 | var self = MochiKit.Iter; | 588 | var self = MochiKit.Iter; |
591 | try { | 589 | try { |
592 | self.ifilter(func, iterable).next(); | 590 | self.ifilter(func, iterable).next(); |
593 | return true; | 591 | return true; |
594 | } catch (e) { | 592 | } catch (e) { |
595 | if (e != self.StopIteration) { | 593 | if (e != self.StopIteration) { |
596 | throw e; | 594 | throw e; |
597 | } | 595 | } |
598 | return false; | 596 | return false; |
599 | } | 597 | } |
600 | }, | 598 | }, |
601 | 599 | ||
602 | /** @id MochiKit.Iter.iextend */ | 600 | /** @id MochiKit.Iter.iextend */ |
603 | iextend: function (lst, iterable) { | 601 | iextend: function (lst, iterable) { |
604 | var m = MochiKit.Base; | 602 | var m = MochiKit.Base; |
605 | var self = MochiKit.Iter; | 603 | var self = MochiKit.Iter; |
606 | if (m.isArrayLike(iterable) && !self.isIterable(iterable)) { | 604 | if (m.isArrayLike(iterable) && !self.isIterable(iterable)) { |
607 | // fast-path for array-like | 605 | // fast-path for array-like |
608 | for (var i = 0; i < iterable.length; i++) { | 606 | for (var i = 0; i < iterable.length; i++) { |