Changeset 13

Show
Ignore:
Timestamp:
10/25/05 17:12:31 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

inor set improvements and fixes

Files:

Legend:

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

    r5 r13  
    2020/** 
    2121    Iterator module providing iteration services. 
    22     There are two main functions iter and fora for iterating over iterable objects. 
    23     An iterable object is an object which has an iterator function which returns an Iterator object
    24     iter iterates over the object synchronously calling a callback for each item
    25     fora does the same but in an asynchronous matter. 
     22    There is one global function iter() which can be used to iterate over iterable objects synchronously or 
     23    if given a callback asynchronously
     24    An iterable object is an object which has an iterator function (__iter__) which returns an Iterator object
     25     
    2626    The Range class is there to create an iterable object over a range of numbers. 
     27     
    2728    @creator                 Jan-Klaas Kollhof 
    2829    @created                2004-12-08 
     
    377378        print("for on Array + iter \t" + testing.timeExec(100,function(){ 
    378379            var s=''; 
    379             for(var item= (i=r.__iter__()).next(); item!==undefined; item=i.next()){ 
     380            for(i=r.__iter__(); item=i.next() !==undefined;){ 
    380381                s+= item; 
    381382                task(); 
  • jsolait/trunk/jsolait/lib/lang.js

    r10 r13  
    3535        var rs=startEndChar; 
    3636        var p= s.indexOf(startEndChar); 
    37         while(p >= 0){ 
     37        while(p >= 0){  
    3838            if(s.charAt(p-1) == "\\"){ 
    3939                rs+=s.slice(0,p+1);                             
  • jsolait/trunk/jsolait/lib/sets.js

    r9 r13  
    2121/** 
    2222    A module providing Sets. 
     23     
    2324    @creator                 Jan-Klaas Kollhof 
    2425    @created                2005-03-11 
     
    4344    }); 
    4445     
     46    /** 
     47        The Set class. 
     48        Items in a class must be Strings, Numbers, Objects that return a unique string representation or 
     49        Objects implementing a __hash__ method. (All objects created from jsolait classes have a __hash__ function. 
     50    **/ 
    4551    mod.Set=Class(function(publ, supr){ 
    4652        /** 
    4753            Initializes a Set instance. 
    48             @param itearatable=[]  A String or an Array of which the elements will be added to the set. 
    49         **/ 
    50         publ.__init__=function(iterable){ 
     54            @param elem*   An element which is added to the set or an Array, String or iterable object of which the elements are added to the set. 
     55        **/ 
     56        publ.__init__=function(elem){ 
     57             
    5158            this.items = {}; 
    52             if(iterable === undefined){ 
    53                 iterable = []; 
    54             } 
    55             if(iterable.constructor == String){ 
    56                 iterable = iterable.split(""); 
    57             } 
    58             //todo do iterable impl 
    59             /* 
    60             if(iterable.__iter__){ 
    61                 var i=iterable.__iter__(); 
    62                 var item; 
    63                 while(item=i.next()!==undefined){ 
    64                     this.add(item); 
    65                 } 
    66             }else{*/ 
    67                 for(var i=0;i<iterable.length;i++){ 
    68                     this.add(iterable[i]); 
    69                 } 
    70             //} 
     59            var elems =[]; 
     60             
     61            if(arguments.length > 1){ 
     62               elems=arguments; 
     63            }else if(arguments.length == 1){ 
     64                elems = arguments[0]; 
     65                if(elems instanceof Array){ 
     66                }else if(typeof elems == "string"){ 
     67                    elems=elems.split(""); 
     68                }else if(elems.__iter__){ 
     69                    var i=iterable.__iter__(); 
     70                    var item; 
     71                    while(item=i.next()!==undefined){ 
     72                        this.add(item); 
     73                    } 
     74                    return; 
     75                }else{ 
     76                    throw new mod.Exception("Array,String or iterable object expected but found %s".format(elems)); 
     77                } 
     78            } 
     79             
     80            for(var i=0;i<elems.length;i++){ 
     81                this.add(elems[i]); 
     82            } 
    7183        }; 
    7284         
     
    223235            return ns; 
    224236        }; 
     237                
    225238         
    226239        /**