Changeset 7638

Show
Ignore:
Timestamp:
03/07/08 23:49:55 (9 months ago)
Author:
cho45
Message:

lang/javascript/jsenumerator/trunk/jsenumerator.js,
lang/javascript/jsenumerator/trunk/test.js:

apply にするかどうかと function の引数のカンマのあるなしにした。Committers:kyo_ago さんのアイデア
殆ど自然に書けるようになった。kyo_ago++

Location:
lang/javascript/jsenumerator/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/jsenumerator/trunk/jsenumerator.js

    r7617 r7638  
    163163         *     E(1, 2, 3).map(function (i) { return i * i }); //=> [1, 4, 9] 
    164164         */ 
    165         map : function (fun, apply) { 
     165        map : function (fun) { 
    166166                var ret = []; 
    167167                try { 
    168168                        if (this.array) { 
    169169                                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])); 
    171171                                } 
    172172                        } else { 
    173                                 while (1) ret.push(fun[apply || "call"](this, this.next())); 
     173                                while (1) ret.push(this._call(fun, this.next())); 
    174174                        } 
    175175                } catch (e) { 
     
    187187         *     E(1, 2, 3).cycle().imap(function (i) { return i * i }).take(6); //=> [1, 4, 9, 1, 4, 9] 
    188188         */ 
    189         imap : function (fun, apply) { 
     189        imap : function (fun) { 
    190190                var self = this; 
    191191                return Enumerator(function () { 
    192                         return fun[apply || "call"](this, self.next()) 
     192                        return this._call(fun, self.next()) 
    193193                }); 
    194194        }, 
     
    225225         *     //=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 
    226226         */ 
    227         iselect : function (fun, apply) { 
     227        iselect : function (fun) { 
    228228                var self = this; 
    229229                return Enumerator(function () { 
    230230                        do { 
    231231                                var val = self.next(); 
    232                         } while (!fun[apply || "call"](this, val)); 
     232                        } while (!this._call(fun, val)); 
    233233                        return val; 
    234234                }); 
     
    250250                do { 
    251251                        var ret = this.next(); 
    252                 } while (!fun[apply || "call"](this, ret)); 
     252                } while (!this._call(fun, ret)); 
    253253                return ret; 
    254254        }, 
     
    341341         * 
    342342         */ 
    343         itake : function (a, apply) { 
     343        itake : function (a) { 
    344344                var self = this; 
    345345                if (typeof(a) == "number") { // take 
     
    355355                        return Enumerator(function () { 
    356356                                var ret = self.next(); 
    357                                 if (a[apply || "call"](this, ret)) 
     357                                if (this._call(a, ret)) 
    358358                                        return ret; 
    359359                                else 
     
    371371         * Receiver must be finite. 
    372372         */ 
    373         take : function (a, apply) { 
    374                 return this.itake(a, apply).toArray(); 
     373        take : function (a) { 
     374                return this.itake(a).toArray(); 
    375375        }, 
    376376 
     
    382382         *     E().countup().idrop(2).take(10); //=> [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 
    383383         */ 
    384         idrop : function (a, apply) { 
     384        idrop : function (a) { 
    385385                var self = this, i; 
    386386                if (typeof(a) == "number") {   // drop 
     
    389389                } else 
    390390                if (typeof(a) == "function") { // dropwhile 
    391                         while (a[apply || "call"](this, i = this.next())) true; 
     391                        while (this._call(a, i = this.next())) true; 
    392392                        return Enumerator(function () { 
    393393                                this.next = self.next; 
     
    406406         * Receiver must be finite. 
    407407         */ 
    408         drop : function (a, apply) { 
    409                 return this.idrop(a, apply).toArray(); 
     408        drop : function (a) { 
     409                return this.idrop(a).toArray(); 
    410410        }, 
    411411 
     
    423423         * Receiver must be finite. 
    424424         */ 
    425         every : function (fun, apply) { 
     425        every : function (fun) { 
    426426                try { 
    427                         while (!(fun[apply || "call"](this, this.next()) === false)) 1; 
     427                        while (!(this._call(fun, this.next()) === false)) 1; 
    428428                        return false; 
    429429                } catch (e) { 
     
    446446         * Receiver must be finite. 
    447447         */ 
    448         some : function (fun, apply) { 
     448        some : function (fun) { 
    449449                try { 
    450                         while (!(fun[apply || "call"](this, this.next()) === true)) 1; 
     450                        while (!(this._call(fun, this.next()) === true)) 1; 
    451451                        return true; 
    452452                } catch (e) { 
     
    464464         *         log(item);  // a, b, c 
    465465         *         log(index); // 0, 1, 2 
    466          *     }, "apply"); 
     466         *     }); 
    467467         */ 
    468468        withIndex : function (start) { 
     
    481481                var start = this.next() || 0; 
    482482                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); 
    483488        } 
    484489}; 
  • lang/javascript/jsenumerator/trunk/test.js

    r7617 r7638  
    234234        expect("apply", [3, 5], E([[1, 2], [2, 3]]).map(function (x, y) { 
    235235                return x + y; 
    236         }, "apply")); 
     236        })); 
    237237}). 
    238238tests("imap", function () { 
     
    241241        }).toArray()); 
    242242 
    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, _) { 
    244244                return i * i; 
    245         }, "apply").toArray()); 
     245        }).toArray()); 
    246246}). 
    247247tests("each", function () { 
     
    261261 
    262262        ret = []; 
    263         E([[ 1 ], [ 2 ], [ 3 ]]).each(function (i) { 
     263        E([[ 1, "a" ], [ 2, "b"], [ 3, "c" ]]).each(function (i, _) { 
    264264                ret.push(i); 
    265         }, "apply"); 
     265        }); 
    266266        expect("Basic", [1, 2, 3], ret); 
    267267 
    268268        ret = []; 
    269         E([[ 1 ], [ 2 ], [ 3 ]]).each(function (i) { 
     269        E([[ 1, "a" ], [ 2, "b"], [ 3, "c" ]]).each(function (i, _) { 
    270270                ret.push(i); 
    271271                throw Enumerator.StopIteration; 
    272         }, "apply"); 
     272        }); 
    273273        expect("Basic", [1], ret); 
    274274}). 
     
    297297        }).toArray()); 
    298298 
    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, _) { 
    300300                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, _) { 
    304304                return i > 3; 
    305         }, "apply").toArray()); 
     305        }).toArray()); 
    306306}). 
    307307tests("find", function () { 
     
    310310        })); 
    311311 
    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, _) { 
    313313                return i > 3; 
    314         }, "apply")); 
     314        })); 
    315315}). 
    316316tests("reduce", function () { 
     
    402402                E(["a", "b", "c", "d"]).izip(E([1, 2, 3]).cycle()).withIndex().iselect(function (item, index) { 
    403403                        return index % 2 == 0; 
    404                 }, "apply").toArray() 
     404                }).toArray() 
    405405        ); 
    406406 
     
    416416                .imap(function (num, fizz, buzz) { 
    417417                        return fizz + buzz || num; 
    418                 }, "apply") 
     418                }) 
    419419                .take(20); 
    420420        expect("FizzBuzz", fizzbuzzA, fizzbuzz);