generated from Meekdai/Gmeek-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AaronChen5651
committed
Dec 18, 2024
1 parent
a48e7ac
commit 24c0cfe
Showing
5 changed files
with
445 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# AARON's BLOG :link: https://AaronChen5651.github.io/aaron.github.io | ||
### :page_facing_up: [4](https://AaronChen5651.github.io/aaron.github.io/tag.html) | ||
### :speech_balloon: 0 | ||
### :hibiscus: 6626 | ||
### :alarm_clock: 2024-12-18 15:47:42 | ||
### :hibiscus: 10269 | ||
### :alarm_clock: 2024-12-18 17:24:34 | ||
### Powered by :heart: [Gmeek](https://github.com/Meekdai/Gmeek) |
190 changes: 189 additions & 1 deletion
190
backup/UC Berkeley CS61A Sping 2021 Lecture 4 Higher Order Functions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,189 @@ | ||
### **Functions on Functions** | ||
### **Functions on Functions** | ||
|
||
**两条原则** | ||
1.函数应该只做一件明确的事(复杂的文档注释说明函数做得太多) | ||
2.DRY(Don’t Repeat Yourself),不要重复,出现重复的代码块建议重构(refactoring),将重复代码块替换成一个单独的函数 | ||
summation举例, 函数作为参数 | ||
函数是第一类值(first-class values),可以作为变量传递给函数,也可以作为函数的返回值。 | ||
|
||
**根据这样的原则,尽量把函数(functions)变成模板(templates)!!** | ||
 | ||
|
||
 | ||
把k**2,替换成term(k),形成模板 | ||
|
||
|
||
--- | ||
--- | ||
|
||
### **Functions that Produce Functions** | ||
|
||
**高阶函数**的概念,重点是**函数可以返回另一个函数**。 | ||
|
||
--- | ||
|
||
### **1. 什么是函数的一等公民?** | ||
|
||
在 Python 中,**函数是一等公民**(First-Class Values),这意味着: | ||
1. **函数可以赋值给变量**。 | ||
2. **函数可以作为参数传递给另一个函数**。 | ||
3. **函数可以作为返回值** 返回给调用者。 | ||
|
||
简单的例子: | ||
```python | ||
def square(x): | ||
return x * x | ||
|
||
f = square # 把函数 square 赋值给变量 f | ||
print(f(4)) # 调用 f,输出 16 | ||
``` | ||
|
||
--- | ||
|
||
### **2. 高阶函数 `add_func`** | ||
|
||
#### **目标**: | ||
我们要定义一个函数 `add_func`,它接收两个函数 `f` 和 `g`,返回一个新的函数 `adder`。这个新函数 `adder` 会把 `f(x)` 和 `g(x)` 的结果加起来。 | ||
|
||
--- | ||
|
||
#### **代码解析**: | ||
|
||
```python | ||
def add_func(f, g): | ||
"""Return function that returns F(x) + G(x) for argument x.""" | ||
def adder(x): # 定义一个新的函数 adder | ||
return f(x) + g(x) # 计算 f(x) 和 g(x) 的和 | ||
return adder # 返回 adder 函数 | ||
``` | ||
|
||
- `add_func(f, g)`: | ||
- 输入两个函数 `f` 和 `g`。 | ||
- 返回一个新的函数 `adder`。 | ||
- `adder(x)`: | ||
- 接收一个参数 `x`。 | ||
- 计算 `f(x)` 和 `g(x)`,然后把它们相加,返回结果。 | ||
|
||
--- | ||
|
||
#### **使用示例**: | ||
|
||
```python | ||
from math import sin, cos, pi | ||
|
||
h = add_func(sin, cos) # 调用 add_func,传入 sin 和 cos 函数 | ||
print(h(pi / 4)) # 调用返回的函数 h,传入 pi/4 | ||
``` | ||
|
||
- **`sin` 和 `cos` 是 Python 内置的数学函数**。 | ||
- `add_func(sin, cos)` 返回的函数 `h` 会计算 `sin(x) + cos(x)`。 | ||
|
||
**具体计算**: | ||
- `x = pi / 4` 时,`sin(pi/4) ≈ 0.707`,`cos(pi/4) ≈ 0.707`。 | ||
- 所以 `sin(pi/4) + cos(pi/4) ≈ 1.414`。 | ||
|
||
**输出**: | ||
``` | ||
1.414213562373095 | ||
``` | ||
|
||
--- | ||
|
||
### **总结这一页的重点**: | ||
|
||
1. **函数可以返回另一个函数**。 | ||
2. **高阶函数** `add_func` 组合了两个函数的结果。 | ||
3. 你可以用 `add_func` 生成一个新的函数(比如 `h`),然后调用这个新函数。 | ||
|
||
--- | ||
|
||
**如何把函数组合逻辑泛化**,即让我们不仅能做加法,还能做其他操作,比如减法、乘法等。 | ||
|
||
--- | ||
|
||
### **1. 问题:如何更通用地组合两个函数?** | ||
|
||
在上一页中,`add_func` 只能将两个函数的结果相加。但我们可以让它更加灵活,支持任意的操作,比如: | ||
- 加法:`f(x) + g(x)` | ||
- 乘法:`f(x) * g(x)` | ||
- 最大值:`max(f(x), g(x))` | ||
|
||
为此,我们定义一个**更通用的函数** `combine_funcs`。 | ||
|
||
--- | ||
|
||
### **2. `combine_funcs` 的代码** | ||
|
||
```python | ||
def combine_funcs(op): | ||
"""General function-combining function.""" | ||
def combined(f, g): # 接收两个函数 f 和 g | ||
def val(x): # 定义一个内部函数 val | ||
return op(f(x), g(x)) # 对 f(x) 和 g(x) 应用操作符 op | ||
return val # 返回 val 函数 | ||
return combined # 返回 combined 函数 | ||
``` | ||
|
||
--- | ||
|
||
#### **代码解释**: | ||
|
||
1. **`combine_funcs(op)`**: | ||
- `op` 是一个操作符,比如 `+`、`*`、`max` 等。 | ||
- 返回一个函数 `combined`。 | ||
|
||
2. **`combined(f, g)`**: | ||
- 接收两个函数 `f` 和 `g`。 | ||
- 返回一个函数 `val`。 | ||
|
||
3. **`val(x)`**: | ||
- 接收一个参数 `x`。 | ||
- 对 `f(x)` 和 `g(x)` 应用操作符 `op`,并返回结果。 | ||
|
||
--- | ||
|
||
### **3. 构建 `add_func`** | ||
|
||
我们可以通过 `combine_funcs` 来构建 `add_func`,使用 `operator.add` 作为操作符。 | ||
|
||
```python | ||
from operator import add # 导入加法操作符 | ||
|
||
add_func = combine_funcs(add) # 使用 combine_funcs 创建 add_func | ||
``` | ||
|
||
- `operator.add` 是 Python 内置的加法操作符。 | ||
- 现在 `add_func` 和上一页的 `add_func` 一样,功能是将两个函数的结果相加。 | ||
|
||
--- | ||
|
||
### **4. 使用示例** | ||
|
||
```python | ||
from math import sin, cos, pi | ||
|
||
h = add_func(sin, cos) # 使用 add_func 组合 sin 和 cos | ||
print(h(pi / 4)) # 调用返回的函数 h,传入 pi/4 | ||
``` | ||
|
||
**输出**: | ||
``` | ||
1.414213562373095 | ||
``` | ||
|
||
--- | ||
|
||
### **总结这一页的重点**: | ||
|
||
1. **`combine_funcs`** 是一个通用的函数组合工具,可以通过操作符 `op` 组合两个函数的结果。 | ||
2. **`operator.add`** 被用来构建 `add_func`。 | ||
3. **函数的组合变得更加灵活**,不仅可以加法,还可以通过不同的操作符做其他操作。 | ||
|
||
--- | ||
|
||
## **整体总结** | ||
|
||
1. **第一页**:展示了如何用函数返回另一个函数,组合两个函数的输出(加法)。 | ||
2. **第二页**:将函数组合逻辑泛化,支持任意操作符,而不仅仅是加法。 | ||
|
||
通过这样的代码设计,你可以构建灵活的函数组合工具,充分体现了**函数式编程**的思想。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"singlePage": [], "startSite": "12/10/2024", "filingNum": "", "onePageListNum": 15, "commentLabelColor": "#006b75", "yearColorList": ["#bc4c00", "#0969da", "#1f883d", "#A333D0"], "i18n": "CN", "themeMode": "manual", "dayTheme": "light", "nightTheme": "dark_colorblind", "urlMode": "pinyin", "script": "", "style": "", "head": "", "indexScript": "", "indexStyle": "", "bottomText": "", "showPostSource": 1, "iconList": {}, "UTC": 8, "rssSplit": "sentence", "exlink": {}, "needComment": 1, "allHead": "", "title": "AARON's BLOG", "subTitle": " ", "avatarUrl": "https://github.githubassets.com/favicons/favicon.svg", "GMEEK_VERSION": "last", "postListJson": {"P1": {"htmlDir": "docs/post/test.html", "labels": ["test"], "postTitle": "test", "postUrl": "post/test.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/1", "commentNum": 0, "wordCount": 4, "description": "test\u3002", "top": 0, "createdAt": 1733794020, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-10", "dateLabelColor": "#bc4c00"}, "P3": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 2 Functions.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 2 Functions", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20%20Lecture%202%20Functions.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/3", "commentNum": 0, "wordCount": 1396, "description": "lecture2\u4e3b\u8981\u8bb2\u89e3\u51fd\u6570\u8c03\u7528\u8fc7\u7a0b\uff0c\u7eaf\u51fd\u6570\u3001\u975e\u7eaf\u51fd\u6570\uff0c\u4ece\u73af\u5883\u89d2\u5ea6\u89e3\u91ca\u51fd\u6570\u8c03\u7528\u7b49\u3002", "top": 0, "createdAt": 1734274908, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-15", "dateLabelColor": "#bc4c00"}, "P4": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 3 Control.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 3 Control", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20Lecture%203%20Control.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/4", "commentNum": 0, "wordCount": 5196, "description": "### **\u73af\u5883\u94fe\u6761\uff08Environment Chain\uff09**\r\nfor example\uff1a\r\n\r\n\r\n---\r\n---\r\n\r\n\r\n### **\u73af\u5883\u4e2d\u7684\u7ed1\u5b9a\u548c\u6c42\u503c\uff08Binding and Evaluation\uff09**\r\n\r\n\r\n\r\n### 1. **\u53d8\u91cf\u8d4b\u503c\uff08Assigning\uff09** \r\n- **\u8d4b\u503c**\uff08Assigning\uff09\u5c06\u4e00\u4e2a\u6570\u503c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002", "top": 0, "createdAt": 1734407281, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-17", "dateLabelColor": "#bc4c00"}, "P5": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 4 Higher Order Functions.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 4 Higher Order Functions", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20Lecture%204%20Higher%20Order%20Functions.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/5", "commentNum": 0, "wordCount": 30, "description": "### **Functions on Functions**\u3002", "top": 0, "createdAt": 1734508037, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-18", "dateLabelColor": "#bc4c00"}}, "singeListJson": {}, "labelColorDict": {"bug": "#d73a4a", "documentation": "#0075ca", "duplicate": "#cfd3d7", "enhancement": "#a2eeef", "good first issue": "#7057ff", "help wanted": "#008672", "invalid": "#e4e669", "lecture note": "#d4c5f9", "question": "#d876e3", "test": "#d93f0b", "wontfix": "#ffffff"}, "displayTitle": "AARON's BLOG", "faviconUrl": "https://github.githubassets.com/favicons/favicon.svg", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "primerCSS": "<link href='https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/Primer/21.0.7/primer.css' rel='stylesheet' />", "homeUrl": "https://AaronChen5651.github.io/aaron.github.io", "prevUrl": "disabled", "nextUrl": "disabled"} | ||
{"singlePage": [], "startSite": "12/10/2024", "filingNum": "", "onePageListNum": 15, "commentLabelColor": "#006b75", "yearColorList": ["#bc4c00", "#0969da", "#1f883d", "#A333D0"], "i18n": "CN", "themeMode": "manual", "dayTheme": "light", "nightTheme": "dark_colorblind", "urlMode": "pinyin", "script": "", "style": "", "head": "", "indexScript": "", "indexStyle": "", "bottomText": "", "showPostSource": 1, "iconList": {}, "UTC": 8, "rssSplit": "sentence", "exlink": {}, "needComment": 1, "allHead": "", "title": "AARON's BLOG", "subTitle": " ", "avatarUrl": "https://github.githubassets.com/favicons/favicon.svg", "GMEEK_VERSION": "last", "postListJson": {"P1": {"htmlDir": "docs/post/test.html", "labels": ["test"], "postTitle": "test", "postUrl": "post/test.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/1", "commentNum": 0, "wordCount": 4, "description": "test\u3002", "top": 0, "createdAt": 1733794020, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-10", "dateLabelColor": "#bc4c00"}, "P3": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 2 Functions.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 2 Functions", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20%20Lecture%202%20Functions.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/3", "commentNum": 0, "wordCount": 1396, "description": "lecture2\u4e3b\u8981\u8bb2\u89e3\u51fd\u6570\u8c03\u7528\u8fc7\u7a0b\uff0c\u7eaf\u51fd\u6570\u3001\u975e\u7eaf\u51fd\u6570\uff0c\u4ece\u73af\u5883\u89d2\u5ea6\u89e3\u91ca\u51fd\u6570\u8c03\u7528\u7b49\u3002", "top": 0, "createdAt": 1734274908, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-15", "dateLabelColor": "#bc4c00"}, "P4": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 3 Control.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 3 Control", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20Lecture%203%20Control.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/4", "commentNum": 0, "wordCount": 5196, "description": "### **\u73af\u5883\u94fe\u6761\uff08Environment Chain\uff09**\r\nfor example\uff1a\r\n\r\n\r\n---\r\n---\r\n\r\n\r\n### **\u73af\u5883\u4e2d\u7684\u7ed1\u5b9a\u548c\u6c42\u503c\uff08Binding and Evaluation\uff09**\r\n\r\n\r\n\r\n### 1. **\u53d8\u91cf\u8d4b\u503c\uff08Assigning\uff09** \r\n- **\u8d4b\u503c**\uff08Assigning\uff09\u5c06\u4e00\u4e2a\u6570\u503c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53d8\u91cf\u4e0a\u3002", "top": 0, "createdAt": 1734407281, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-17", "dateLabelColor": "#bc4c00"}, "P5": {"htmlDir": "docs/post/UC Berkeley CS61A Sping 2021 Lecture 4 Higher Order Functions.html", "labels": ["lecture note"], "postTitle": "UC Berkeley CS61A Sping 2021 Lecture 4 Higher Order Functions", "postUrl": "post/UC%20Berkeley%20CS61A%20Sping%202021%20Lecture%204%20Higher%20Order%20Functions.html", "postSourceUrl": "https://github.com/AaronChen5651/aaron.github.io/issues/5", "commentNum": 0, "wordCount": 3673, "description": "### **Functions on Functions**\r\n\r\n**\u4e24\u6761\u539f\u5219**\r\n1.\u51fd\u6570\u5e94\u8be5\u53ea\u505a\u4e00\u4ef6\u660e\u786e\u7684\u4e8b\uff08\u590d\u6742\u7684\u6587\u6863\u6ce8\u91ca\u8bf4\u660e\u51fd\u6570\u505a\u5f97\u592a\u591a\uff09\r\n2.DRY(Don\u2019t Repeat Yourself)\uff0c\u4e0d\u8981\u91cd\u590d\uff0c\u51fa\u73b0\u91cd\u590d\u7684\u4ee3\u7801\u5757\u5efa\u8bae\u91cd\u6784\uff08refactoring\uff09\uff0c\u5c06\u91cd\u590d\u4ee3\u7801\u5757\u66ff\u6362\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\r\nsummation\u4e3e\u4f8b, \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\r\n\u51fd\u6570\u662f\u7b2c\u4e00\u7c7b\u503c(first-class values)\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u53d8\u91cf\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002", "top": 0, "createdAt": 1734508037, "style": "", "script": "", "head": "", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "createdDate": "2024-12-18", "dateLabelColor": "#bc4c00"}}, "singeListJson": {}, "labelColorDict": {"bug": "#d73a4a", "documentation": "#0075ca", "duplicate": "#cfd3d7", "enhancement": "#a2eeef", "good first issue": "#7057ff", "help wanted": "#008672", "invalid": "#e4e669", "lecture note": "#d4c5f9", "question": "#d876e3", "test": "#d93f0b", "wontfix": "#ffffff"}, "displayTitle": "AARON's BLOG", "faviconUrl": "https://github.githubassets.com/favicons/favicon.svg", "ogImage": "https://github.githubassets.com/favicons/favicon.svg", "primerCSS": "<link href='https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/Primer/21.0.7/primer.css' rel='stylesheet' />", "homeUrl": "https://AaronChen5651.github.io/aaron.github.io", "prevUrl": "disabled", "nextUrl": "disabled"} |
Oops, something went wrong.