- Timestamp:
- 07/12/06 09:39:52 (2 years ago)
- Files:
-
- trunk/jsolait/lib/iter.js (modified) (4 diffs)
- trunk/jsolait/lib/strings.js (modified) (2 diffs)
- trunk/test/test_iter.js (modified) (5 diffs)
- trunk/test/test_strings.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/jsolait/lib/iter.js
r52 r64 69 69 var item; 70 70 while(((item=this.next()) !== undefined) && result===undefined){ 71 result=cb.call(thisObj, item, this); 71 if(item.__tupleResult__){ 72 item.push(this); 73 result=cb.apply(thisObj, item); 74 }else{ 75 result=cb.call(thisObj, item, this); 76 } 72 77 } 73 78 return result; … … 77 82 var result=[]; 78 83 thisObj = thisObj==null?this:thisObj; 79 var item ;84 var item, doKeep; 80 85 while((item=this.next()) !== undefined){ 81 if(cb.call(thisObj, item, this)){ 86 if(item.__tupleResult__){ 87 item.push(this); 88 doKeep=cb.apply(thisObj, item); 89 }else{ 90 doKeep=cb.call(thisObj, item, this); 91 } 92 if(doKeep){ 82 93 result.push(item); 83 94 } … … 89 100 var result=[]; 90 101 thisObj = thisObj==null?this:thisObj; 91 var item ;102 var item, mapedItem; 92 103 while((item=this.next()) !== undefined){ 93 result.push(cb.call(thisObj, item, this)); 104 if(item.__tupleResult__){ 105 item.push(this); 106 mapedItem=cb.apply(thisObj, item); 107 }else{ 108 mapedItem=cb.call(thisObj, item, this); 109 } 110 result.push(mapedItem); 94 111 } 95 112 return result; … … 340 357 return mod.iter(iterable).__list__(); 341 358 }; 359 360 /** 361 An itereator that iterates over a number of given iterators. 362 use zip() to create a Zipper. 363 **/ 364 mod.Zipper = Class(mod.Iterator, function(publ,priv,supr){ 365 366 publ.__init__=function(iterators){ 367 this.iterators = iterators; 368 }; 369 370 publ.next=function(){ 371 var r =[]; 372 r.__tupleResult__ = true; 373 var item; 374 for(var i=0;i<this.iterators.length;i++){ 375 item=this.iterators[i].next(); 376 if(item === undefined){ 377 return undefined; 378 }else{ 379 r.push(item); 380 } 381 } 382 return r; 383 }; 384 }); 385 386 /** 387 Creates an iterator which iterates over the iterable objects given simultanously. 388 @param iterable* Any number of iterable objects. 389 @return a Zipper iterator. 390 **/ 391 mod.zip = function(iterable){ 392 var iterators =[]; 393 for(var i=0;i<arguments.length;i++){ 394 iterators.push(mod.iter(arguments[i])); 395 } 396 return new mod.Zipper(iterators); 397 }; 342 398 343 399 }); trunk/jsolait/lib/strings.js
r51 r64 1 1 /** 2 Module providing String manipulationfunctionality (String templates, Fast writable Strings, ...).2 Module providing String functionality (String templates, Fast writable Strings, ...). 3 3 4 4 @creator Jan-Klaas Kollhof … … 8 8 **/ 9 9 Module("strings","$Revision: 43$", function(mod){ 10 11 mod.WordNumberStringSplitter = Class(function(publ,priv,supr){ 12 publ.__init__=function(s){ 13 this.s=s; 14 }; 15 16 publ.next=function(){ 17 if(this.s.length==0){ 18 return; 19 } 20 var m = this.s.match(/^(\s*[0-9]+\s*)/); 21 22 if(m){ 23 this.s=this.s.slice(m[1].length); 24 return Number(m[1]); 25 }else{ 26 m = this.s.match(/^([^0-9]+)/); 27 if(m){ 28 this.s=this.s.slice(m[1].length); 29 return m[1].replace(" ","") 30 }else{ 31 return; 32 } 33 } 34 }; 35 }); 36 37 mod.naturalCompare=function(a, b){ 38 var asplitter=new mod.WordNumberStringSplitter(a); 39 var bsplitter=new mod.WordNumberStringSplitter(b); 40 41 while(true){ 42 var x = asplitter.next(); 43 var y = bsplitter.next(); 44 if(x<y){ 45 return -1; 46 }else if(x>y){ 47 return 1; 48 }else if(x == null && y == null){ 49 return 0; 50 } 51 } 52 }; 53 54 10 55 /** 11 56 Fast writable String. trunk/test/test_iter.js
r50 r64 9 9 var map = imprt('iter').map; 10 10 var list = imprt('iter').list; 11 var zip = imprt('iter').zip; 11 12 12 13 … … 63 64 }); 64 65 t.assertEquals('map(range(0,20), item + 2) == 2 .. 22', n.join(","), a.join(",")); 66 67 68 var a=[1,2,3,4,5]; 69 var b=[5,4,3,2]; 70 var r = map(zip(a,b), function(a,b){ 71 return a + ':' + b; 72 }); 73 t.assertEquals('zip([1,2,3,4,5], [5,4,3,2])', r.join(","), "1:5,2:4,3:3,4:2") 65 74 }; 66 75 … … 69 78 var iter = imprt('iter').iter; 70 79 var filter = imprt('iter').filter; 80 var range = imprt('iter').range; 71 81 72 82 var testing = imprt('testing'); 73 83 74 mod.__test__(testing);84 75 85 76 86 var task=function(){ … … 96 106 print("Range iter \t\t" + testing.profile(function(){ 97 107 var s=[]; 98 iter( mod.range(0,99), function(item,i){108 iter(range(0,99), function(item,i){ 99 109 s.push(r[item]); 100 110 task(); … … 174 184 })); 175 185 186 187 188 176 189 }; 177 190 mod.__main__=function(){ trunk/test/test_strings.js
r50 r64 2 2 3 3 mod.test=function(testing, logger){ 4 imprt('strings');4 var strings = imprt('strings'); 5 5 6 6 logger.log("testing strings"); … … 10 10 11 11 12 var rslt = tmpl.exec({name:"test", b:2}) 12 var rslt = tmpl.exec({name:"test", b:2}); 13 13 testing.assertEquals("template result", rslt, "Template (test) run at Thu Jan 1 00:00:00 UTC 1970\n\n loop 0\n\n loop 1\n\n loop 2\n\n loop 3\n\n loop 4\n\n loop 5\n\n loop 6\n\n loop 7\n\n loop 8\n\n loop 9\n\n------------\n\n b is set ? > <?\n foo\n\n------------\n0,1,2,3,4,5,6,7,8,9,\n-----------\n") 14 14 15 16 var a = ["jsolait 1","jsolait 11", "jsolait 2"]; 17 a.sort(strings.naturalCompare); 18 testing.assertEquals("natural compare", a.join(", "), "jsolait 1, jsolait 2, jsolait 11"); 15 19 }; 16 20
