Created
April 5, 2011 18:15
-
-
Save Quby/904166 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(_){ | |
_.zip = function(){ | |
var i,u,res=[],list; | |
for(i=0;i<arguments[0].length;i++){ | |
list = []; | |
for(u=0;u<arguments.length;u++){ | |
list.push(arguments[u][i]); | |
} | |
res.push(list); | |
} | |
return res; | |
}; | |
_.zipWith = function(){ | |
var i,u,res=[],list; | |
for(i=0;i<arguments[1].length;i++){ | |
list = []; | |
for(u=1;u<arguments.length;u++){ | |
list.push(arguments[u][i]); | |
} | |
res.push(arguments[0].apply(this,list)); | |
} | |
return res; | |
}; | |
_.fn = function(body){ | |
body = body.split('->'); | |
return new Function(body[0],"return "+body[1]) | |
}; | |
_.compose = function(){ | |
var args = arguments; | |
return args.length>1?function(x){ | |
return compose.apply(this,Array.prototype.slice.call(args,1))(args[0](x)) | |
}:args[0]; | |
}; | |
_.and = function(){ | |
var args = arguments; | |
return args.length==2?function(){ return args[1].apply(null,arguments) && args[0].apply(null,arguments)}:function(){ | |
return args[0].apply(null,arguments) && and(Array.prototype.slice.call(args)); | |
} | |
}; | |
_.or = function(){ | |
var args = arguments; | |
return args.length==2?function(){ return args[1].apply(null,arguments) || args[0].apply(null,arguments)}:function(){ | |
return args[0].apply(null,arguments) || and(Array.prototype.slice.call(args,1)); | |
} | |
}; | |
_.xor = function(){ | |
var args = arguments; | |
return args.length==2?function(){ return args[1].apply(null,arguments) != args[0].apply(null,arguments)}:function(){ | |
return args[0].apply(null,arguments) != and(Array.prototype.slice.call(args,1)); | |
} | |
}; | |
_.not = function(fn){ | |
return function(){ | |
return !fn.apply(null,arguments); | |
} | |
}; | |
_.range = function(init,iter){ | |
if(!iter){ | |
iter = function(a){ | |
return a+1; | |
} | |
}else if(typeof iter == 'number'){ | |
i = iter; | |
iter = function(a){ | |
return a+i; | |
} | |
} | |
return sequence([init],1,iter); | |
}; | |
_.sequence = function(init,n,iter){ | |
return { | |
take:function(x){ | |
var list=init.slice(0); | |
while(x>list.length){ | |
list.push(iter.apply(null,(n!=Infinity?list.slice(list.length-n):list).concat(list.length))); | |
} | |
return list | |
}, | |
read:function(x){ | |
return this.take(x).pop(); | |
}, | |
generator:function(){ | |
return { | |
list:init.clone(), | |
start:function(){ | |
delete this.list | |
this.list = init.clone(); | |
}, | |
next:function(){ | |
this.list.push(iter.apply(null,(n!=Infinity?this.list.slice(this.list.length-n):this.list).concat(this.list.length))); | |
return this.list[this.list.length-1]; | |
} | |
} | |
} | |
} | |
}; | |
Function.prototype.twice = function(x){ | |
var self = this; | |
if(!x || x===2){ | |
return function(a){ | |
return self(self(a)); | |
}; | |
}else{ | |
return function(a){ | |
return self(self.twice(x-1)(a)); | |
}; | |
} | |
}; | |
Function.prototype.flip = function(m1,m2){ | |
m1 = m1.split(','); | |
m2 = m2.split(','); | |
var fn = this,i; | |
return function(){ | |
var argsObj = {},args = []; | |
for(i=0;i<arguments.length&&i<m1.length;i++){ | |
argsObj[m1[i]] = arguments[i]; | |
} | |
for(i=0;i<m2.length;i++){ | |
args[i] = argsObj[m2[i]]; | |
} | |
return fn.apply(null,args); | |
}; | |
}; | |
Function.prototype.curry = function(){ | |
var args = Array.prototype.slice.call(arguments), | |
fn = this; | |
return function(){ | |
return fn.apply(null,Array.prototype.concat.call([],Array.prototype.slice.call(arguments),args)); | |
}; | |
}; | |
Array.prototype.foldl = function(fn,init){ | |
var i = typeof init=='undefined'?(init=this[0],1):0; | |
for(;i<this.length;i++){ | |
init = fn(init,this[i]); | |
} | |
return init; | |
} | |
Array.prototype.foldr = function(fn,init){ | |
var i = this.length - typeof init=='undefined'?(init=this[this.length-1],1):0; | |
for(;i--;){ | |
init = fn(init,this[i]); | |
} | |
return init; | |
} | |
Array.prototype.scanl = function(fn,init){ | |
var list = (init || []).concat(this); | |
for(var i=1;i<list.length;i++){ | |
list[i] = fn(list[i-1],list[i]); | |
} | |
return list; | |
} | |
Array.prototype.scanr = function(fn,init){ | |
var list = this.concat(init || []); | |
for(var i=list.length;i--;){ | |
list[i-1] = fn(list[i],list[i-1]); | |
} | |
return list; | |
} | |
Array.prototype.map = function(fn){ | |
var list = [],i=0; | |
for(;i<this.length;i++){ | |
list.push(fn(this[i])); | |
} | |
return list; | |
} | |
Array.prototype.each = function(fn){ | |
for(var i=0;i<this.length;i++){ | |
fn(this[i]); | |
} | |
} | |
Array.prototype.filter = function(fn){ | |
return this.foldl(function(list,elem){ | |
return list.concat(fn(elem)?elem:[]); | |
},[]); | |
} | |
Array.prototype.any = function(fn){ | |
return this.foldl(function(a,b){ | |
return a || fn(b); | |
},false); | |
} | |
Array.prototype.every = function(fn){ | |
return this.foldl(function(a,b){ | |
return a && fn(b); | |
},true); | |
} | |
Array.prototype.has = function(e){ | |
return this.any(function(a){return a==e}) | |
} | |
Array.prototype.head = function(){ | |
return this[0]; | |
} | |
Array.prototype.tail = function(){ | |
return this.slice(1); | |
} | |
Array.prototype.clone = function(){ | |
return this.slice(0); | |
} | |
Array.prototype.unique = function(fn){ | |
fn = fn || function(a,b){return a==b} | |
var list=[],i=0,t; | |
for(;i<this.length;i++){ | |
was = false; | |
for(t=0;t<list.length;t++){ | |
if(fn(this[i],list[t])){ | |
was = true; | |
break; | |
} | |
} | |
if(!was)list.push(this[i]); | |
} | |
return list; | |
} | |
Array.prototype.group = function(fn){ | |
fn = fn || function(a,b){return typeof a == typeof b} | |
var list = [],i=0,t,r; | |
for(;i<this.length;i++){ | |
r = false; | |
for(t=0;t<list.length;t++){ | |
if(fn(list[t][0],this[i])){ | |
r = true; | |
list[t].push(this[i]); | |
break; | |
} | |
} | |
if(!r){ | |
list.push([this[i]]); | |
} | |
} | |
return list; | |
} | |
Math.add = function(a,b){ | |
return a+b; | |
} | |
Math.sub = function(a,b){ | |
return a-b; | |
} | |
Math.mul = function(a,b){ | |
return a*b; | |
} | |
Math.div = function(a,b){ | |
return a/b; | |
} | |
Math.mod = function(a,b){ | |
return a%b; | |
} | |
_.Cmp = { | |
eq:function(a,b){ | |
return a==b; | |
}, | |
less:function(a,b){ | |
return a<b; | |
}, | |
more:function(a,b){ | |
return a>b; | |
} | |
} | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment