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,59 +1,59 @@ | |||
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 */ |
@@ -177,163 +177,161 @@ MochiKit.Base.update(MochiKit.Iter, { | |||
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 | } |
@@ -368,97 +366,97 @@ MochiKit.Base.update(MochiKit.Iter, { | |||
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] */) { |