2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7b8d367417b2b2512b5b
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 学习函数式编程
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
2020-08-05 16:38:04 +08:00
|
|
|
|
forumTopicId: 301233
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: learn-about-functional-programming
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
函数式编程是一种方案简单、功能独立、对作用域外没有任何副作用的编程范式。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`INPUT -> PROCESS -> OUTPUT`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
函数式编程:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
1)功能独立——不依赖于程序的状态(比如可能发生变化的全局变量);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2)纯函数——同一个输入永远能得到同一个输出;
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
3)有限的副作用——可以严格地限制函数外部对状态的更改。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
freeCodeCamp 成员在 love tea 的故事。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
在代码编辑器中,已经为你定义好了`prepareTea`和`getTea`函数。调用`getTea`函数为团队准备 40 杯茶,并将它们存储在`tea4TeamFCC`变量里。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`tea4TeamFCC`变量里应有 40 杯为团队准备的茶。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(tea4TeamFCC.length === 40);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`tea4TeamFCC`变量里应有 greenTea。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(tea4TeamFCC[0] === 'greenTea');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Function that returns a string representing a cup of green tea
|
|
|
|
|
const prepareTea = () => 'greenTea';
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Given a function (representing the tea type) and number of cups needed, the
|
|
|
|
|
following function returns an array of strings (each representing a cup of
|
|
|
|
|
a specific type of tea).
|
|
|
|
|
*/
|
|
|
|
|
const getTea = (numOfCups) => {
|
|
|
|
|
const teaCups = [];
|
|
|
|
|
|
|
|
|
|
for(let cups = 1; cups <= numOfCups; cups += 1) {
|
|
|
|
|
const teaCup = prepareTea();
|
|
|
|
|
teaCups.push(teaCup);
|
|
|
|
|
}
|
|
|
|
|
return teaCups;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
const tea4TeamFCC = null;
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
const prepareTea = () => 'greenTea';
|
|
|
|
|
|
|
|
|
|
const getTea = (numOfCups) => {
|
|
|
|
|
const teaCups = [];
|
|
|
|
|
|
|
|
|
|
for(let cups = 1; cups <= numOfCups; cups += 1) {
|
|
|
|
|
const teaCup = prepareTea();
|
|
|
|
|
teaCups.push(teaCup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return teaCups;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const tea4TeamFCC = getTea(40);
|
|
|
|
|
```
|