javascript中的curry

curry 用来组合function还是8错的
	//curry function
	Function.prototype.curry=function(){
		var originFunc=this;
		var args=[];
		for(var i=0;i<arguments.length;i++){
			args[i]=arguments[i];
		}
		if(args.length==0) args=null;
		var newFunc=function(){
			var args=[];
			for(var i=0;i<arguments.length;i++){
				args[i]=arguments[i];
			}
			args=arguments.callee.curryArgs.concat(args);
			if(args.length==0)args=null;
			return arguments.callee.originFunc.apply(this,args);
		}
		newFunc.curryArgs=args;
		newFunc.originFunc=originFunc;
		return newFunc;
	}
	//each function for array . test curry
	Array.prototype.each=function(f){
		for(var i=0;i<this.length;i++){
			f(this[i]);
		}
	}
	//map function for array,generate new array, test curry
	Array.prototype.map=function(f){
		var result=[];
		for(var i=0;i<this.length;i++){
			result[i]=f(this[i]);
		}
		return result;
	}
	//test run
	function run(){
		
		var composit=function(f,g,x){
			return f(g(x));
		}
		
		var mul=function(x,y){
			return x*y;
		}
		//var triple=mul.curry(3);
		//alert(triple(4));
		var double=mul.curry(2);
		//var sixTimes=composit.curry(double).curry(triple);
		//var al6=composit.curry(alert).curry(sixTimes);
		//al6(10);
		
		var app=function(f,list){
			list.each(f);
		}
		var map=function(f,list){
			return list.map(f);
		}
		var l=[8,7,6];
		l.alertEach=l.each.curry(alert);
		l.alertEach();
		app.curry(alert)(l);
		composit.curry(alert,map.curry(double))(l);
	}
	run();

你可能感兴趣的:(JavaScript,prototype,IBM,Blog,groovy)