Skip to content

Commit

Permalink
lang/javascript/jsenumerator/trunk/jsenumerator.mini.js,
Browse files Browse the repository at this point in the history
lang/javascript/jsenumerator/trunk/jsenumerator.nodoc.js:
    リリースファイル生成してなかった


git-svn-id: http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk@8144 d0d07461-0603-4401-acd4-de1884942a52
  • Loading branch information
cho45 committed Mar 19, 2008
1 parent 223f1d1 commit bc396a5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
65 changes: 36 additions & 29 deletions jsenumerator.mini.js
@@ -1,16 +1,17 @@
// JSEnumerator 0.0.0 (c) Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ )
// JSEnumerator 0.0.0 Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ )
// http://coderepos.org/share/wiki/JSEnumerator
function Enumerator(a){
return(arguments.length>1)? new Enumerator().initWithArray(arguments):
(this instanceof Enumerator)? this.init(a):new Enumerator(a);}
Enumerator.prototype={
init:function(){
if(arguments.length==0){
this.initWithArray([]);}else{
if(arguments[0] && arguments[0].length){
this.initWithArray(arguments[0]);}else
if(typeof arguments[0]=="function"){
this.initWithFunction(arguments[0]);}else
if(typeof arguments[0]=="object"){
if(arguments[0].hasOwnProperty("length")){
this.initWithArray(arguments[0]);}else
if(arguments[0] instanceof Enumerator){
return arguments[0];}else{
this.initWithHash(arguments[0]);}
Expand Down Expand Up @@ -38,31 +39,35 @@ try{
var i=self.next();cache.push(i);return i;}catch(e){
if(e !=Enumerator.StopIteration)throw e;var i=-1;this.next=function(){return cache[++i % cache.length]};return this.next();}
});},
map:function(fun,apply){
map:function(fun){
var ret=[];try{
if(this.array){
for(;this.pos<this.array.length;this.pos++){
ret.push(fun[apply || "call"](this,this.array[this.pos]));}
}else{
while(1)ret.push(fun[apply || "call"](this,this.next()));}
var a=this.array,c=this.pos,len=a.length-c,i=len % 8,type=(fun.length>1)? "apply":"call";if(i>0)do{
ret.push(fun[type](this,a[c++]));}while(--i);i=parseInt(len>>3);if(i>0)do{
ret.push(
fun[type](this,a[c++]),fun[type](this,a[c++]),
fun[type](this,a[c++]),fun[type](this,a[c++]),
fun[type](this,a[c++]),fun[type](this,a[c++]),
fun[type](this,a[c++]),fun[type](this,a[c++])
);}while(--i);this.pos=c;}else{
while(1)ret.push(fun[fun.length>1 ? "apply":"call"](this,this.next()));}
}catch(e){
if(e !=Enumerator.StopIteration)throw e;}
return ret;},
imap:function(fun,apply){
imap:function(fun){
var self=this;return Enumerator(function(){
return fun[apply || "call"](this,self.next())
});},
return fun[fun.length>1 ? "apply":"call"](this,self.next());});},
izip:function(){
var eles=[this];eles.push.apply(eles,Enumerator(arguments).map(function(i){
return Enumerator(i);}));return Enumerator(function(){
var args=[];for(var i=0;i<eles.length;i++)args.push(eles[i].next());return args;});},
iselect:function(fun,apply){
iselect:function(fun){
var self=this;return Enumerator(function(){
do{
var val=self.next();}while(!fun[apply || "call"](this,val));return val;});},
find:function(fun,apply){
var val=self.next();}while(!fun[fun.length>1 ? "apply":"call"](this,val));return val;});},
find:function(fun){
do{
var ret=this.next();}while(!fun[apply || "call"](this,ret));return ret;},
var ret=this.next();}while(!fun[fun.length>1 ? "apply":"call"](this,ret));return ret;},
reduce:function(fun,init){
var self=this;var rval=(typeof init=="undefined")? self.next():init;this.each(function(i){rval=fun.call(this,rval,i)});return rval;},
max:function(fun){
Expand All @@ -78,41 +83,43 @@ try{
return f.next();}catch(e){
if(e !=Enumerator.StopIteration)throw e;f=a.next();return f.next();}
});},
itake:function(a,apply){
itake:function(a){
var self=this;if(typeof(a)=="number"){
var i=0;return Enumerator(function(){
if(i++<a)
return self.next();else
throw Enumerator.StopIteration;});}else
if(typeof(a)=="function"){
return Enumerator(function(){
var ret=self.next();if(a[apply || "call"](this,ret))
var ret=self.next();if(a[a.length>1 ? "apply":"call"](this,ret))
return ret;else
throw Enumerator.StopIteration;});return ret;}
throw Enumerator.StopIteration;});}
},
take:function(a,apply){
return this.itake(a,apply).toArray();},
idrop:function(a,apply){
take:function(a){
return this.itake(a).toArray();},
idrop:function(a){
var self=this,i;if(typeof(a)=="number"){
for(i=0;i<a;i++)this.next();return this;}else
if(typeof(a)=="function"){
while(a[apply || "call"](this,i=this.next()))true;return Enumerator(function(){
while(a[a.length>1 ? "apply":"call"](this,i=this.next()))true;return Enumerator(function(){
this.next=self.next;return i;});}
},
drop:function(a,apply){
return this.idrop(a,apply).toArray();},
every:function(fun,apply){
drop:function(a){
return this.idrop(a).toArray();},
every:function(fun){
try{
while(!(fun[apply || "call"](this,this.next())===false))1;return false;}catch(e){
while(!(fun[fun.length>1 ? "apply":"call"](this,this.next())===false))1;return false;}catch(e){
if(e !=Enumerator.StopIteration)throw e;return true;}
},
some:function(fun,apply){
some:function(fun){
try{
while(!(fun[apply || "call"](this,this.next())===true))1;return true;}catch(e){
while(!(fun[fun.length>1 ? "apply":"call"](this,this.next())===true))1;return true;}catch(e){
if(e !=Enumerator.StopIteration)throw e;return false;}
},
withIndex:function(start){
return this.izip(E(start || 0).countup());},
countup:function(){
var start=this.next()|| 0;return Enumerator(function(){return start++});}
var start=this.next()|| 0;return Enumerator(function(){return start++});},
stop:function(){
throw Enumerator.StopIteration;}
};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");
72 changes: 44 additions & 28 deletions jsenumerator.nodoc.js
@@ -1,4 +1,5 @@
// JSEnumerator 0.0.0 (c) Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ )
// JSEnumerator 0.0.0 Copyright (c) 2008 KAYAC Inc. ( http://www.kayac.com/ )
// http://coderepos.org/share/wiki/JSEnumerator
function Enumerator (a) {
return (arguments.length > 1) ? new Enumerator().initWithArray(arguments) :
(this instanceof Enumerator) ? this.init(a) : new Enumerator(a);
Expand All @@ -8,13 +9,13 @@ Enumerator.prototype = {
if (arguments.length == 0) {
this.initWithArray([]);
} else {
if (arguments[0] && arguments[0].length) {
this.initWithArray(arguments[0]);
} else
if (typeof arguments[0] == "function") {
this.initWithFunction(arguments[0]);
} else
if (typeof arguments[0] == "object") {
if (arguments[0].hasOwnProperty("length")) {
this.initWithArray(arguments[0]);
} else
if (arguments[0] instanceof Enumerator) {
return arguments[0];
} else {
Expand Down Expand Up @@ -76,15 +77,26 @@ Enumerator.prototype = {
},


map : function (fun, apply) {
map : function (fun) {
var ret = [];
try {
if (this.array) {
for (; this.pos < this.array.length; this.pos++) {
ret.push(fun[apply || "call"](this, this.array[this.pos]));
}
var a = this.array, c = this.pos, len = a.length - c, i = len % 8, type = (fun.length > 1) ? "apply" : "call";
if (i > 0) do {
ret.push(fun[type](this, a[c++]));
} while (--i);
i = parseInt(len >> 3);
if (i > 0) do {
ret.push(
fun[type](this, a[c++]), fun[type](this, a[c++]),
fun[type](this, a[c++]), fun[type](this, a[c++]),
fun[type](this, a[c++]), fun[type](this, a[c++]),
fun[type](this, a[c++]), fun[type](this, a[c++])
);
} while (--i);
this.pos = c;
} else {
while (1) ret.push(fun[apply || "call"](this, this.next()));
while (1) ret.push(fun[fun.length > 1 ? "apply" : "call"](this, this.next()));
}
} catch (e) {
if (e != Enumerator.StopIteration) throw e;
Expand All @@ -93,10 +105,10 @@ Enumerator.prototype = {
},


imap : function (fun, apply) {
imap : function (fun) {
var self = this;
return Enumerator(function () {
return fun[apply || "call"](this, self.next())
return fun[fun.length > 1 ? "apply" : "call"](this, self.next());
});
},

Expand All @@ -114,21 +126,21 @@ Enumerator.prototype = {
},


iselect : function (fun, apply) {
iselect : function (fun) {
var self = this;
return Enumerator(function () {
do {
var val = self.next();
} while (!fun[apply || "call"](this, val));
} while (!fun[fun.length > 1 ? "apply" : "call"](this, val));
return val;
});
},


find : function (fun, apply) {
find : function (fun) {
do {
var ret = this.next();
} while (!fun[apply || "call"](this, ret));
} while (!fun[fun.length > 1 ? "apply" : "call"](this, ret));
return ret;
},

Expand Down Expand Up @@ -171,7 +183,7 @@ Enumerator.prototype = {
},


itake : function (a, apply) {
itake : function (a) {
var self = this;
if (typeof(a) == "number") {
var i = 0;
Expand All @@ -185,30 +197,29 @@ Enumerator.prototype = {
if (typeof(a) == "function") {
return Enumerator(function () {
var ret = self.next();
if (a[apply || "call"](this, ret))
if (a[a.length > 1 ? "apply" : "call"](this, ret))
return ret;
else
throw Enumerator.StopIteration;
});
return ret;
}
},



take : function (a, apply) {
return this.itake(a, apply).toArray();
take : function (a) {
return this.itake(a).toArray();
},


idrop : function (a, apply) {
idrop : function (a) {
var self = this, i;
if (typeof(a) == "number") {
for (i = 0; i < a; i++) this.next();
return this;
} else
if (typeof(a) == "function") {
while (a[apply || "call"](this, i = this.next())) true;
while (a[a.length > 1 ? "apply" : "call"](this, i = this.next())) true;
return Enumerator(function () {
this.next = self.next;
return i;
Expand All @@ -217,14 +228,14 @@ Enumerator.prototype = {
},


drop : function (a, apply) {
return this.idrop(a, apply).toArray();
drop : function (a) {
return this.idrop(a).toArray();
},


every : function (fun, apply) {
every : function (fun) {
try {
while (!(fun[apply || "call"](this, this.next()) === false)) 1;
while (!(fun[fun.length > 1 ? "apply" : "call"](this, this.next()) === false)) 1;
return false;
} catch (e) {
if (e != Enumerator.StopIteration) throw e;
Expand All @@ -233,9 +244,9 @@ Enumerator.prototype = {
},


some : function (fun, apply) {
some : function (fun) {
try {
while (!(fun[apply || "call"](this, this.next()) === true)) 1;
while (!(fun[fun.length > 1 ? "apply" : "call"](this, this.next()) === true)) 1;
return true;
} catch (e) {
if (e != Enumerator.StopIteration) throw e;
Expand All @@ -252,6 +263,11 @@ Enumerator.prototype = {
countup : function () {
var start = this.next() || 0;
return Enumerator(function () { return start++ });
},


stop : function () {
throw Enumerator.StopIteration;
}
};

Expand Down

0 comments on commit bc396a5

Please sign in to comment.