--- id: 587d7dab367417b2b2512b70 title: Introduction to Currying and Partial Application challengeType: 1 videoUrl: '' localeTitle: Currying和Partial Application简介 --- ## Description
函数的arity是它需要的参数数量。 Currying一个功能装置,以n的函数转换arity为N的功能arity 1。换言之,它重构的功能,因此采用一个参数,然后返回另一个函数,它的下一个参数,依此类推。这是一个例子:
//非咖喱功能
function unCurried(x,y){
返回x + y;
}

// Curried功能
function curried(x){
return函数(y){
返回x + y;
}
}
curried(1)(2)//返回3
如果您无法一次为函数提​​供所有参数,则在程序中这很有用。您可以将每个函数调用保存到一个变量中,该变量将保存返回的函数引用,该引用在可用时接受下一个参数。以下是使用上面示例中的curried函数的示例:
//在部分中调用curried函数:
var funcForY = curried(1);
的console.log(funcForY(2)); //打印3
类似地, partial application可以描述为一次向函数应用一些参数并返回应用于更多参数的另一个函数。这是一个例子:
//公正的功能
功能公正(x,y,z){
return x + y + z;
}
var partialFn = impartial.bind(this,1,2);
partialFn(10); //返回13
## Instructions
填写add函数的主体,以便使用currying来添加参数xyz
## Tests
```yml tests: - text: add(10)(20)(30)应该返回60 。 testString: 'assert(add(10)(20)(30) === 60, "add(10)(20)(30) should return 60.");' - text: add(1)(2)(3)应该返回6 。 testString: 'assert(add(1)(2)(3) === 6, "add(1)(2)(3) should return 6.");' - text: add(11)(22)(33)应该返回66 。 testString: 'assert(add(11)(22)(33) === 66, "add(11)(22)(33) should return 66.");' - text: 您的代码应包含返回x + y + z的final语句。 testString: 'assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g), "Your code should include a final statement that returns x + y + z.");' ```
## Challenge Seed
```js function add(x) { // Add your code below this line // Add your code above this line } add(10)(20)(30); ```
## Solution
```js // solution required ```