Changeset 7638
- Timestamp:
- 03/07/08 23:49:55 (9 months ago)
- Location:
- lang/javascript/jsenumerator/trunk
- Files:
-
- 2 modified
-
jsenumerator.js (modified) (14 diffs)
-
test.js (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/javascript/jsenumerator/trunk/jsenumerator.js
r7617 r7638 163 163 * E(1, 2, 3).map(function (i) { return i * i }); //=> [1, 4, 9] 164 164 */ 165 map : function (fun , apply) {165 map : function (fun) { 166 166 var ret = []; 167 167 try { 168 168 if (this.array) { 169 169 for (; this.pos < this.array.length; this.pos++) { 170 ret.push( fun[apply || "call"](this, this.array[this.pos]));170 ret.push(this._call(fun, this.array[this.pos])); 171 171 } 172 172 } else { 173 while (1) ret.push( fun[apply || "call"](this, this.next()));173 while (1) ret.push(this._call(fun, this.next())); 174 174 } 175 175 } catch (e) { … … 187 187 * E(1, 2, 3).cycle().imap(function (i) { return i * i }).take(6); //=> [1, 4, 9, 1, 4, 9] 188 188 */ 189 imap : function (fun , apply) {189 imap : function (fun) { 190 190 var self = this; 191 191 return Enumerator(function () { 192 return fun[apply || "call"](this, self.next())192 return this._call(fun, self.next()) 193 193 }); 194 194 }, … … 225 225 * //=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 226 226 */ 227 iselect : function (fun , apply) {227 iselect : function (fun) { 228 228 var self = this; 229 229 return Enumerator(function () { 230 230 do { 231 231 var val = self.next(); 232 } while (! fun[apply || "call"](this, val));232 } while (!this._call(fun, val)); 233 233 return val; 234 234 }); … … 250 250 do { 251 251 var ret = this.next(); 252 } while (! fun[apply || "call"](this, ret));252 } while (!this._call(fun, ret)); 253 253 return ret; 254 254 }, … … 341 341 * 342 342 */ 343 itake : function (a , apply) {343 itake : function (a) { 344 344 var self = this; 345 345 if (typeof(a) == "number") { // take … … 355 355 return Enumerator(function () { 356 356 var ret = self.next(); 357 if ( a[apply || "call"](this, ret))357 if (this._call(a, ret)) 358 358 return ret; 359 359 else … … 371 371 * Receiver must be finite. 372 372 */ 373 take : function (a , apply) {374 return this.itake(a , apply).toArray();373 take : function (a) { 374 return this.itake(a).toArray(); 375 375 }, 376 376 … … 382 382 * E().countup().idrop(2).take(10); //=> [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 383 383 */ 384 idrop : function (a , apply) {384 idrop : function (a) { 385 385 var self = this, i; 386 386 if (typeof(a) == "number") { // drop … … 389 389 } else 390 390 if (typeof(a) == "function") { // dropwhile 391 while ( a[apply || "call"](this, i = this.next())) true;391 while (this._call(a, i = this.next())) true; 392 392 return Enumerator(function () { 393 393 this.next = self.next; … … 406 406 * Receiver must be finite. 407 407 */ 408 drop : function (a , apply) {409 return this.idrop(a , apply).toArray();408 drop : function (a) { 409 return this.idrop(a).toArray(); 410 410 }, 411 411 … … 423 423 * Receiver must be finite. 424 424 */ 425 every : function (fun , apply) {425 every : function (fun) { 426 426 try { 427 while (!( fun[apply || "call"](this, this.next()) === false)) 1;427 while (!(this._call(fun, this.next()) === false)) 1; 428 428 return false; 429 429 } catch (e) { … … 446 446 * Receiver must be finite. 447 447 */ 448 some : function (fun , apply) {448 some : function (fun) { 449 449 try { 450 while (!( fun[apply || "call"](this, this.next()) === true)) 1;450 while (!(this._call(fun, this.next()) === true)) 1; 451 451 return true; 452 452 } catch (e) { … … 464 464 * log(item); // a, b, c 465 465 * log(index); // 0, 1, 2 466 * } , "apply");466 * }); 467 467 */ 468 468 withIndex : function (start) { … … 481 481 var start = this.next() || 0; 482 482 return Enumerator(function () { return start++ }); 483 }, 484 485 _call : function (fun, arg) { 486 var m = (fun.toString().split("{",2)[0].indexOf(",") == -1) ? "call" : "apply"; 487 return fun[m](this, arg); 483 488 } 484 489 }; -
lang/javascript/jsenumerator/trunk/test.js
r7617 r7638 234 234 expect("apply", [3, 5], E([[1, 2], [2, 3]]).map(function (x, y) { 235 235 return x + y; 236 } , "apply"));236 })); 237 237 }). 238 238 tests("imap", function () { … … 241 241 }).toArray()); 242 242 243 expect("apply", [1, 4, 9], E([[1 ], [2], [3]]).imap(function (i) {243 expect("apply", [1, 4, 9], E([[1, "a"], [2, "b"], [3, "c"]]).imap(function (i, _) { 244 244 return i * i; 245 } , "apply").toArray());245 }).toArray()); 246 246 }). 247 247 tests("each", function () { … … 261 261 262 262 ret = []; 263 E([[ 1 ], [ 2 ], [ 3 ]]).each(function (i) {263 E([[ 1, "a" ], [ 2, "b"], [ 3, "c" ]]).each(function (i, _) { 264 264 ret.push(i); 265 } , "apply");265 }); 266 266 expect("Basic", [1, 2, 3], ret); 267 267 268 268 ret = []; 269 E([[ 1 ], [ 2 ], [ 3 ]]).each(function (i) {269 E([[ 1, "a" ], [ 2, "b"], [ 3, "c" ]]).each(function (i, _) { 270 270 ret.push(i); 271 271 throw Enumerator.StopIteration; 272 } , "apply");272 }); 273 273 expect("Basic", [1], ret); 274 274 }). … … 297 297 }).toArray()); 298 298 299 expect("Basic", [[ 4 ], [ 5 ]], E([[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]).iselect(function (i ) {299 expect("Basic", [[ 4 ], [ 5 ]], E([[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]).iselect(function (i, _) { 300 300 return i > 3; 301 } , "apply").toArray());302 303 expect("Basic", [[ 4 ], [ 5 ]], E([[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]).ifilter(function (i ) {301 }).toArray()); 302 303 expect("Basic", [[ 4 ], [ 5 ]], E([[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]).ifilter(function (i, _) { 304 304 return i > 3; 305 } , "apply").toArray());305 }).toArray()); 306 306 }). 307 307 tests("find", function () { … … 310 310 })); 311 311 312 expect("apply", [ 4 ], E([[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]).find(function (i) {312 expect("apply", [ 4, "d" ], E([[ 1, "a"], [ 2, "b" ], [ 3, "c" ], [ 4, "d" ], [ 5, "e" ]]).find(function (i, _) { 313 313 return i > 3; 314 } , "apply"));314 })); 315 315 }). 316 316 tests("reduce", function () { … … 402 402 E(["a", "b", "c", "d"]).izip(E([1, 2, 3]).cycle()).withIndex().iselect(function (item, index) { 403 403 return index % 2 == 0; 404 } , "apply").toArray()404 }).toArray() 405 405 ); 406 406 … … 416 416 .imap(function (num, fizz, buzz) { 417 417 return fizz + buzz || num; 418 } , "apply")418 }) 419 419 .take(20); 420 420 expect("FizzBuzz", fizzbuzzA, fizzbuzz);
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)