javascript 高阶函数 currying & uncurrying

* currying

var currying = function(fn) {
	var args = [];

	return function() {
		if (arguments.length === 0) {
			return fn.apply(this, args);
		} else {
			[].push.apply(args, arguments);
			return arguments.callee;
		}
	}
}

var cost = (function() {	
	var money = 0;

	return function() {
		for (var i = 0, len = arguments.length; i < len; i++) {
			money += arguments[i];
		}
		return money;
	}
}).call(this);

// 转化为currying函数
var cost = currying(cost);

// 未真正求值
cost(100);
cost(200);
cost(300);

// 求值并输出
console.log(cost());

  output: 600

 

* uncurrying

 

posted @ 2018-08-02 15:31  zhanghui_ming  阅读(134)  评论(0编辑  收藏  举报