Changeset 8144 for lang/javascript
 Timestamp:
 03/19/08 17:18:38 (8 years ago)
 Location:
 lang/javascript/jsenumerator/trunk
 Files:

 2 modified
Legend:
 Unmodified
 Added
 Removed

lang/javascript/jsenumerator/trunk/jsenumerator.mini.js
r7292 r8144 1 // JSEnumerator 0.0.0 (c) Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ ) 1 // JSEnumerator 0.0.0 Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ ) 2 // http://coderepos.org/share/wiki/JSEnumerator 2 3 function Enumerator(a){ 3 4 return(arguments.length>1)? new Enumerator().initWithArray(arguments): … … 7 8 if(arguments.length==0){ 8 9 this.initWithArray([]);}else{ 10 if(arguments[0] && arguments[0].length){ 11 this.initWithArray(arguments[0]);}else 9 12 if(typeof arguments[0]=="function"){ 10 13 this.initWithFunction(arguments[0]);}else 11 14 if(typeof arguments[0]=="object"){ 12 if(arguments[0].hasOwnProperty("length")){13 this.initWithArray(arguments[0]);}else14 15 if(arguments[0] instanceof Enumerator){ 15 16 return arguments[0];}else{ … … 39 40 if(e !=Enumerator.StopIteration)throw e;var i=1;this.next=function(){return cache[++i % cache.length]};return this.next();} 40 41 });}, 41 map:function(fun ,apply){42 map:function(fun){ 42 43 var ret=[];try{ 43 44 if(this.array){ 44 for(;this.pos<this.array.length;this.pos++){ 45 ret.push(fun[apply  "call"](this,this.array[this.pos]));} 46 }else{ 47 while(1)ret.push(fun[apply  "call"](this,this.next()));} 45 var a=this.array,c=this.pos,len=a.lengthc,i=len % 8,type=(fun.length>1)? "apply":"call";if(i>0)do{ 46 ret.push(fun[type](this,a[c++]));}while(i);i=parseInt(len>>3);if(i>0)do{ 47 ret.push( 48 fun[type](this,a[c++]),fun[type](this,a[c++]), 49 fun[type](this,a[c++]),fun[type](this,a[c++]), 50 fun[type](this,a[c++]),fun[type](this,a[c++]), 51 fun[type](this,a[c++]),fun[type](this,a[c++]) 52 );}while(i);this.pos=c;}else{ 53 while(1)ret.push(fun[fun.length>1 ? "apply":"call"](this,this.next()));} 48 54 }catch(e){ 49 55 if(e !=Enumerator.StopIteration)throw e;} 50 56 return ret;}, 51 imap:function(fun ,apply){57 imap:function(fun){ 52 58 var self=this;return Enumerator(function(){ 53 return fun[apply  "call"](this,self.next()) 54 });}, 59 return fun[fun.length>1 ? "apply":"call"](this,self.next());});}, 55 60 izip:function(){ 56 61 var eles=[this];eles.push.apply(eles,Enumerator(arguments).map(function(i){ 57 62 return Enumerator(i);}));return Enumerator(function(){ 58 63 var args=[];for(var i=0;i<eles.length;i++)args.push(eles[i].next());return args;});}, 59 iselect:function(fun ,apply){64 iselect:function(fun){ 60 65 var self=this;return Enumerator(function(){ 61 66 do{ 62 var val=self.next();}while(!fun[ apply "call"](this,val));return val;});},63 find:function(fun ,apply){67 var val=self.next();}while(!fun[fun.length>1 ? "apply":"call"](this,val));return val;});}, 68 find:function(fun){ 64 69 do{ 65 var ret=this.next();}while(!fun[ apply "call"](this,ret));return ret;},70 var ret=this.next();}while(!fun[fun.length>1 ? "apply":"call"](this,ret));return ret;}, 66 71 reduce:function(fun,init){ 67 72 var self=this;var rval=(typeof init=="undefined")? self.next():init;this.each(function(i){rval=fun.call(this,rval,i)});return rval;}, … … 79 84 if(e !=Enumerator.StopIteration)throw e;f=a.next();return f.next();} 80 85 });}, 81 itake:function(a ,apply){86 itake:function(a){ 82 87 var self=this;if(typeof(a)=="number"){ 83 88 var i=0;return Enumerator(function(){ … … 87 92 if(typeof(a)=="function"){ 88 93 return Enumerator(function(){ 89 var ret=self.next();if(a[a pply "call"](this,ret))94 var ret=self.next();if(a[a.length>1 ? "apply":"call"](this,ret)) 90 95 return ret;else 91 throw Enumerator.StopIteration;}); return ret;}96 throw Enumerator.StopIteration;});} 92 97 }, 93 take:function(a ,apply){94 return this.itake(a ,apply).toArray();},95 idrop:function(a ,apply){98 take:function(a){ 99 return this.itake(a).toArray();}, 100 idrop:function(a){ 96 101 var self=this,i;if(typeof(a)=="number"){ 97 102 for(i=0;i<a;i++)this.next();return this;}else 98 103 if(typeof(a)=="function"){ 99 while(a[a pply "call"](this,i=this.next()))true;return Enumerator(function(){104 while(a[a.length>1 ? "apply":"call"](this,i=this.next()))true;return Enumerator(function(){ 100 105 this.next=self.next;return i;});} 101 106 }, 102 drop:function(a ,apply){103 return this.idrop(a ,apply).toArray();},104 every:function(fun ,apply){107 drop:function(a){ 108 return this.idrop(a).toArray();}, 109 every:function(fun){ 105 110 try{ 106 while(!(fun[ apply "call"](this,this.next())===false))1;return false;}catch(e){111 while(!(fun[fun.length>1 ? "apply":"call"](this,this.next())===false))1;return false;}catch(e){ 107 112 if(e !=Enumerator.StopIteration)throw e;return true;} 108 113 }, 109 some:function(fun ,apply){114 some:function(fun){ 110 115 try{ 111 while(!(fun[ apply "call"](this,this.next())===true))1;return true;}catch(e){116 while(!(fun[fun.length>1 ? "apply":"call"](this,this.next())===true))1;return true;}catch(e){ 112 117 if(e !=Enumerator.StopIteration)throw e;return false;} 113 118 }, … … 115 120 return this.izip(E(start  0).countup());}, 116 121 countup:function(){ 117 var start=this.next() 0;return Enumerator(function(){return start++});} 122 var start=this.next() 0;return Enumerator(function(){return start++});}, 123 stop:function(){ 124 throw Enumerator.StopIteration;} 118 125 };Enumerator.prototype.to_a=Enumerator.prototype.toArray;Enumerator.prototype.each=Enumerator.prototype.map;Enumerator.prototype.inject=Enumerator.prototype.reduce;Enumerator.prototype.ifilter=Enumerator.prototype.iselect;Enumerator.StopIteration=new Error("StopIteration"); 
lang/javascript/jsenumerator/trunk/jsenumerator.nodoc.js
r7292 r8144 1 // JSEnumerator 0.0.0 (c) Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ ) 1 // JSEnumerator 0.0.0 Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ ) 2 // http://coderepos.org/share/wiki/JSEnumerator 2 3 function Enumerator (a) { 3 4 return (arguments.length > 1) ? new Enumerator().initWithArray(arguments) : … … 9 10 this.initWithArray([]); 10 11 } else { 12 if (arguments[0] && arguments[0].length) { 13 this.initWithArray(arguments[0]); 14 } else 11 15 if (typeof arguments[0] == "function") { 12 16 this.initWithFunction(arguments[0]); 13 17 } else 14 18 if (typeof arguments[0] == "object") { 15 if (arguments[0].hasOwnProperty("length")) {16 this.initWithArray(arguments[0]);17 } else18 19 if (arguments[0] instanceof Enumerator) { 19 20 return arguments[0]; … … 77 78 78 79 79 map : function (fun , apply) {80 map : function (fun) { 80 81 var ret = []; 81 82 try { 82 83 if (this.array) { 83 for (; this.pos < this.array.length; this.pos++) { 84 ret.push(fun[apply  "call"](this, this.array[this.pos])); 85 } 84 var a = this.array, c = this.pos, len = a.length  c, i = len % 8, type = (fun.length > 1) ? "apply" : "call"; 85 if (i > 0) do { 86 ret.push(fun[type](this, a[c++])); 87 } while (i); 88 i = parseInt(len >> 3); 89 if (i > 0) do { 90 ret.push( 91 fun[type](this, a[c++]), fun[type](this, a[c++]), 92 fun[type](this, a[c++]), fun[type](this, a[c++]), 93 fun[type](this, a[c++]), fun[type](this, a[c++]), 94 fun[type](this, a[c++]), fun[type](this, a[c++]) 95 ); 96 } while (i); 97 this.pos = c; 86 98 } else { 87 while (1) ret.push(fun[ apply "call"](this, this.next()));99 while (1) ret.push(fun[fun.length > 1 ? "apply" : "call"](this, this.next())); 88 100 } 89 101 } catch (e) { … … 94 106 95 107 96 imap : function (fun , apply) {97 var self = this; 98 return Enumerator(function () { 99 return fun[ apply  "call"](this, self.next())108 imap : function (fun) { 109 var self = this; 110 return Enumerator(function () { 111 return fun[fun.length > 1 ? "apply" : "call"](this, self.next()); 100 112 }); 101 113 }, … … 115 127 116 128 117 iselect : function (fun , apply) {129 iselect : function (fun) { 118 130 var self = this; 119 131 return Enumerator(function () { 120 132 do { 121 133 var val = self.next(); 122 } while (!fun[ apply "call"](this, val));134 } while (!fun[fun.length > 1 ? "apply" : "call"](this, val)); 123 135 return val; 124 136 }); … … 126 138 127 139 128 find : function (fun , apply) {140 find : function (fun) { 129 141 do { 130 142 var ret = this.next(); 131 } while (!fun[ apply "call"](this, ret));143 } while (!fun[fun.length > 1 ? "apply" : "call"](this, ret)); 132 144 return ret; 133 145 }, … … 172 184 173 185 174 itake : function (a , apply) {186 itake : function (a) { 175 187 var self = this; 176 188 if (typeof(a) == "number") { … … 186 198 return Enumerator(function () { 187 199 var ret = self.next(); 188 if (a[a pply "call"](this, ret))200 if (a[a.length > 1 ? "apply" : "call"](this, ret)) 189 201 return ret; 190 202 else 191 203 throw Enumerator.StopIteration; 192 204 }); 193 return ret; 194 } 195 }, 196 197 198 199 take : function (a, apply) { 200 return this.itake(a, apply).toArray(); 201 }, 202 203 204 idrop : function (a, apply) { 205 } 206 }, 207 208 209 210 take : function (a) { 211 return this.itake(a).toArray(); 212 }, 213 214 215 idrop : function (a) { 205 216 var self = this, i; 206 217 if (typeof(a) == "number") { … … 209 220 } else 210 221 if (typeof(a) == "function") { 211 while (a[a pply "call"](this, i = this.next())) true;222 while (a[a.length > 1 ? "apply" : "call"](this, i = this.next())) true; 212 223 return Enumerator(function () { 213 224 this.next = self.next; … … 218 229 219 230 220 drop : function (a , apply) {221 return this.idrop(a , apply).toArray();222 }, 223 224 225 every : function (fun , apply) {231 drop : function (a) { 232 return this.idrop(a).toArray(); 233 }, 234 235 236 every : function (fun) { 226 237 try { 227 while (!(fun[ apply "call"](this, this.next()) === false)) 1;238 while (!(fun[fun.length > 1 ? "apply" : "call"](this, this.next()) === false)) 1; 228 239 return false; 229 240 } catch (e) { … … 234 245 235 246 236 some : function (fun , apply) {247 some : function (fun) { 237 248 try { 238 while (!(fun[ apply "call"](this, this.next()) === true)) 1;249 while (!(fun[fun.length > 1 ? "apply" : "call"](this, this.next()) === true)) 1; 239 250 return true; 240 251 } catch (e) { … … 253 264 var start = this.next()  0; 254 265 return Enumerator(function () { return start++ }); 266 }, 267 268 269 stop : function () { 270 throw Enumerator.StopIteration; 255 271 } 256 272 };