Changeset 64

Show
Ignore:
Timestamp:
07/12/06 09:39:52 (2 years ago)
Author:
Jan-Klaas Kollhof
Message:

adding zip to iter module and naturla compare algorithm to strings module

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jsolait/lib/iter.js

    r52 r64  
    6969            var item; 
    7070            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                } 
    7277            } 
    7378            return result; 
     
    7782            var result=[]; 
    7883            thisObj = thisObj==null?this:thisObj; 
    79             var item
     84            var item, doKeep
    8085            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){ 
    8293                    result.push(item); 
    8394                } 
     
    89100            var result=[]; 
    90101            thisObj = thisObj==null?this:thisObj; 
    91             var  item
     102            var  item, mapedItem
    92103            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); 
    94111            } 
    95112            return result; 
     
    340357        return mod.iter(iterable).__list__(); 
    341358    }; 
     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    }; 
    342398 
    343399}); 
  • trunk/jsolait/lib/strings.js

    r51 r64  
    11/** 
    2     Module providing String manipulation functionality (String templates, Fast writable Strings, ...). 
     2    Module providing String functionality (String templates, Fast writable Strings, ...). 
    33     
    44    @creator Jan-Klaas Kollhof 
     
    88**/ 
    99Module("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     
    1055    /** 
    1156        Fast writable String.  
  • trunk/test/test_iter.js

    r50 r64  
    99        var map = imprt('iter').map; 
    1010        var list = imprt('iter').list; 
     11        var zip = imprt('iter').zip; 
    1112         
    1213         
     
    6364        }); 
    6465        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") 
    6574    }; 
    6675     
     
    6978        var iter = imprt('iter').iter; 
    7079        var filter = imprt('iter').filter; 
     80        var range = imprt('iter').range; 
    7181         
    7282        var  testing = imprt('testing'); 
    7383         
    74         mod.__test__(testing); 
     84        
    7585         
    7686        var task=function(){ 
     
    96106        print("Range iter \t\t" + testing.profile(function(){ 
    97107            var s=[]; 
    98             iter(mod.range(0,99), function(item,i){ 
     108            iter(range(0,99), function(item,i){ 
    99109                s.push(r[item]); 
    100110                task(); 
     
    174184        })); 
    175185     
     186         
     187         
     188     
    176189    }; 
    177190    mod.__main__=function(){ 
  • trunk/test/test_strings.js

    r50 r64  
    22 
    33    mod.test=function(testing, logger){ 
    4         imprt('strings'); 
     4        var strings = imprt('strings'); 
    55         
    66        logger.log("testing strings"); 
     
    1010         
    1111         
    12         var rslt = tmpl.exec({name:"test", b:2}) 
     12        var rslt = tmpl.exec({name:"test", b:2}); 
    1313        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                
    1415         
     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"); 
    1519    }; 
    1620