慕仙森
Currying将n个参数的单个函数转换为n个函数,每个函数都有一个参数。鉴于以下功能:function f(x,y,z) { z(x(y));}咖喱变成:function f(x) { lambda(y) { lambda(z) { z(x(y)); } } }为了获得f(x,y,z)的完整应用,您需要这样做:f(x)(y)(z);许多函数式语言都可以让你写f x y z。如果你只调用f x y或f(x)(y)那么你得到一个部分应用的函数 - 返回值是一个闭包,lambda(z){z(x(y))}传入x和y的值f(x,y)。使用部分应用程序的一种方法是将函数定义为广义函数的部分应用程序,如fold:function fold(combineFunction, accumulator, list) {/* ... */}function sum = curry(fold)(lambda(accum,e){e+accum}))(0);function length = curry(fold)(lambda(accum,_){1+accum})(empty-list);function reverse = curry(fold)(lambda(accum,e){concat(e,accum)})(empty-list);/* ... */@list = [1, 2, 3, 4]sum(list) //returns 10@f = fold(lambda(accum,e){e+accum}) //f = lambda(accumulator,list) {/*...*/}f(0,list) //returns 10@g = f(0) //same as sumg(list) //returns 10