freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../functional-programming/introduction-to-currying-an...

2.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7dab367417b2b2512b70 Introduction to Currying and Partial Application 1 Currying和Partial Application简介

Description

函数的arity是它需要的参数数量。 Currying一个功能装置以n的函数转换arity为N的功能arity 1。换言之它重构的功能因此采用一个参数然后返回另一个函数它的下一个参数依此类推。这是一个例子
//非咖喱功能
function unCurriedxy{
返回x + y;
}

// Curried功能
function curriedx{
return函数y{
返回x + y;
}
}
curried12//返回3
如果您无法一次为函数提​​供所有参数,则在程序中这很有用。您可以将每个函数调用保存到一个变量中,该变量将保存返回的函数引用,该引用在可用时接受下一个参数。以下是使用上面示例中的curried函数的示例:
//在部分中调用curried函数
var funcForY = curried1;
的console.logfuncForY2; //打印3
类似地, partial application可以描述为一次向函数应用一些参数并返回应用于更多参数的另一个函数。这是一个例子:
//公正的功能
功能公正xyz{
return x + y + z;
}
var partialFn = impartial.bindthis1,2;
partialFn10; //返回13

Instructions

填写add函数的主体以便使用currying来添加参数x yz

Tests

tests:
  - text: <code>add(10)(20)(30)</code>应该返回<code>60</code> 。
    testString: 'assert(add(10)(20)(30) === 60, "<code>add(10)(20)(30)</code> should return <code>60</code>.");'
  - text: <code>add(1)(2)(3)</code>应该返回<code>6</code> 。
    testString: 'assert(add(1)(2)(3) === 6, "<code>add(1)(2)(3)</code> should return <code>6</code>.");'
  - text: <code>add(11)(22)(33)</code>应该返回<code>66</code> 。
    testString: 'assert(add(11)(22)(33) === 66, "<code>add(11)(22)(33)</code> should return <code>66</code>.");'
  - text: 您的代码应包含返回<code>x + y + z</code>的final语句。
    testString: 'assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g), "Your code should include a final statement that returns <code>x + y + z</code>.");'

Challenge Seed

function add(x) {
  // Add your code below this line


  // Add your code above this line
}
add(10)(20)(30);

Solution

// solution required