Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
add-elements-to-the-end-of-an-array-using-concat-instead-of-push.chinese.md
apply-functional-programming-to-convert-strings-to-url-slugs.chinese.md
avoid-mutations-and-side-effects-using-functional-programming.chinese.md
combine-an-array-into-a-string-using-the-join-method.chinese.md
combine-two-arrays-using-the-concat-method.chinese.md
implement-map-on-a-prototype.chinese.md
implement-the-filter-method-on-a-prototype.chinese.md
introduction-to-currying-and-partial-application.chinese.md
learn-about-functional-programming.chinese.md
pass-arguments-to-avoid-external-dependence-in-a-function.chinese.md
refactor-global-variables-out-of-functions.chinese.md
remove-elements-from-an-array-using-slice-instead-of-splice.chinese.md
return-a-sorted-array-without-changing-the-original-array.chinese.md
return-part-of-an-array-using-the-slice-method.chinese.md
sort-an-array-alphabetically-using-the-sort-method.chinese.md
split-a-string-into-an-array-using-the-split-method.chinese.md
understand-functional-programming-terminology.chinese.md
understand-the-hazards-of-using-imperative-code.chinese.md
use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.chinese.md
use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.chinese.md
use-the-filter-method-to-extract-data-from-an-array.chinese.md
use-the-map-method-to-extract-data-from-an-array.chinese.md
use-the-reduce-method-to-analyze-data.chinese.md
use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.chinese.md
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
comment-dictionary.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/functional-programming/introduction-to-currying-and-partial-application.chinese.md

107 lines
2.5 KiB
Markdown
Raw Normal View History

---
id: 587d7dab367417b2b2512b70
title: Introduction to Currying and Partial Application
challengeType: 1
forumTopicId: 301232
localeTitle: 函数柯里化
---
## Description
<section id='description'>
<code>arity</code>是函数所需的形参的数量。函数<code>柯里化</code>意思是把接受多个<code>arity</code>的函数变换成接受单一<code>arity</code>的函数。
换句话说,就是重构函数让它接收一个参数,然后返回接收下一个参数的函数,依此类推。
举个例子:
```js
//Un-curried function
function unCurried(x, y) {
return x + y;
}
//柯里化函数
function curried(x) {
return function(y) {
return x + y;
}
}
//Alternative using ES6
const curried = x => y => x + y
curried(1)(2) // 返回 3
```
柯里化在不能一次为函数提供所有参数情况下很有用。因为它可以将每个函数的调用保存到一个变量中,该变量将保存返回的函数引用,该引用在下一个参数可用时接受该参数。下面是使用<code>柯里化</code>函数的例子:
```js
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
```
类似地,<code>局部应用</code>的意思是一次对一个函数应用几个参数,然后返回另一个应用更多参数的函数。
举个例子:
```js
//Impartial function
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
```
</section>
## Instructions
<section id='instructions'>
填写<code>add</code>函数主体部分,用柯里化添加参数<code>x</code><code>y</code><code>z</code>.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>add(10)(20)(30)</code>应返回<code>60</code>
testString: assert(add(10)(20)(30) === 60);
- text: <code>add(1)(2)(3)</code>应返回<code>6</code>
testString: assert(add(1)(2)(3) === 6);
- text: <code>add(11)(22)(33)</code>应返回<code>66</code>
testString: assert(add(11)(22)(33) === 66);
- text: 应返回<code>x + y + z</code>的最终结果。
testString: assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function add(x) {
// Add your code below this line
// Add your code above this line
}
add(10)(20)(30);
```
</div>
</section>
## Solution
<section id='solution'>
```js
const add = x => y => z => x + y + z
```
</section>