init higherOrderFunctions
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
# 中文笔记
|
||||
|
||||
- [引用和值](reference&value.md)
|
||||
- [高阶函数专题](higherOrderFunctions.md)
|
||||
90
note_cn/higherOrderFunctions.md
Normal file
90
note_cn/higherOrderFunctions.md
Normal file
@@ -0,0 +1,90 @@
|
||||
# 高阶函数
|
||||
|
||||
在编程语言中, 我们会将一些常用的操作步骤封装起来, 给它个名字. 下次需要用到该操作时, 只需要通过这个名字来调用即可. 这种特性我们称之为函数, 其作为一种基本的抽象手段, 可以将我们从繁琐细碎的低层面操作中解脱出来.
|
||||
|
||||
为了让这种抽象手段更加通用, 函数的参数应该也可以是一个函数, 函数的返回值, 也可以是一个函数. 这种参数与返回值涉及到其他函数的函数, 我们就称之为**高阶函数**, 也被称之为**函子**.
|
||||
|
||||
高阶函数要比函数更加强悍, 极大程度上增强了编程语言的表现力.
|
||||
|
||||
### 函数作为参数
|
||||
|
||||
首先, 我们来看一下最简单的一个自然数求和函数:
|
||||
|
||||
```python
|
||||
def sum_naturals(n):
|
||||
total, k = 0, 1;
|
||||
while k <= n:
|
||||
total, k = total + k, k + 1
|
||||
return total
|
||||
```
|
||||
|
||||
然后, 需求有点变化, 让你计算立方根的和:
|
||||
|
||||
```python
|
||||
def sum_cubes(n):
|
||||
total, k = 0, 1
|
||||
while k <= n:
|
||||
total, k = total + k*k*k, k + 1
|
||||
return total
|
||||
```
|
||||
|
||||
接着, 需求又变了, 让你计算下图所示形式的和:
|
||||
|
||||

|
||||
|
||||
这是一种慢速求解 PI 的方法.
|
||||
|
||||
```python
|
||||
def pi_sum(n):
|
||||
total, k = 0, 1
|
||||
while k <= n:
|
||||
total, k = total + 8 / ((4*k-3) * (4*k-1)), k + 1
|
||||
return total
|
||||
```
|
||||
|
||||
此时此刻, 想必你已经复制粘贴三次并修改代码了. 其实我们完全可以抽象出一个更加通用的模版:
|
||||
|
||||
```python
|
||||
def <name>(n):
|
||||
total, k = 0, 1
|
||||
while k <= n:
|
||||
total, k = total + <term>(k), k + 1
|
||||
return total
|
||||
```
|
||||
|
||||
抽象出各种 term 的求和过程.
|
||||
|
||||
```python
|
||||
# template (high-order functions)
|
||||
def summation(n, term):
|
||||
total, k = 0, 1
|
||||
while k <= n:
|
||||
total, k = total + term(k), k + 1
|
||||
return total
|
||||
|
||||
# functions
|
||||
def identity(x):
|
||||
return x
|
||||
|
||||
def cube(x):
|
||||
return x*x*x
|
||||
|
||||
def pi_term(x):
|
||||
return 8 / ((4*x-3) * (4*x-1))
|
||||
|
||||
# refactoring
|
||||
def sum_naturals(n):
|
||||
return summation(n, identity)
|
||||
|
||||
def sum_cubes(n):
|
||||
return summation(n, cube)
|
||||
|
||||
def pi_sum(n):
|
||||
return summation(n, pi_term)
|
||||
|
||||
# test
|
||||
print(sum_naturals(10)) #55
|
||||
print(sum_cubes(3)) #36
|
||||
print(pi_sum(1e6)) #3.141592153589902
|
||||
```
|
||||
|
||||
7
note_cn/reference&value.md
Normal file
7
note_cn/reference&value.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# 引用与值
|
||||
|
||||
几乎在任何语言里, 这两者的区分都是组最基础的重点.
|
||||
|
||||
Python 里的区别不露痕迹, 对于 `x = y` 这样的式子, x 到底是引用, 还是值, 完全取决于 y 的类型. 如果 y 是字符串, 数值, 元组这种**不可变类型**, x 就是值. 而如果 y 是列表, 字典这样的**可变类型**而言, x 就是引用.
|
||||
|
||||
对于列表而言, 如果你想要 copy 值, 就需要显式的调用 `copy()` 方法, 如果要更彻底一些, 连 list 里包含的子 list 也要一起 copy, 那么则需要用到 `deepcopy()` 方法.
|
||||
14
practice_projects/characterPictureGrid.py
Normal file
14
practice_projects/characterPictureGrid.py
Normal file
@@ -0,0 +1,14 @@
|
||||
grid = [['.', '.', '.', '.', '.', '.'],
|
||||
['.', 'O', 'O', '.', '.', '.'],
|
||||
['O', 'O', 'O', 'O', '.', '.'],
|
||||
['O', 'O', 'O', 'O', 'O', '.'],
|
||||
['.', 'O', 'O', 'O', 'O', 'O'],
|
||||
['O', 'O', 'O', 'O', 'O', '.'],
|
||||
['O', 'O', 'O', 'O', '.', '.'],
|
||||
['.', 'O', 'O', '.', '.', '.'],
|
||||
['.', '.', '.', '.', '.', '.']]
|
||||
|
||||
for i in range(len(grid[0])):
|
||||
for l in grid:
|
||||
print(l[i], end="")
|
||||
print()
|
||||
14
practice_projects/commaConnection.py
Normal file
14
practice_projects/commaConnection.py
Normal file
@@ -0,0 +1,14 @@
|
||||
spam = ['apple', 'bananas', 'tofu', 'cats']
|
||||
|
||||
def list2Str(l):
|
||||
if not l:
|
||||
return ''
|
||||
if len(l) == 1:
|
||||
return l[0]
|
||||
s = ''
|
||||
for i in range(len(l) - 1):
|
||||
s += l[i] + ', '
|
||||
s += 'and ' + l[-1]
|
||||
return s
|
||||
|
||||
print(list2Str(spam))
|
||||
0
practice_questions/ch04/README.md
Normal file
0
practice_questions/ch04/README.md
Normal file
Reference in New Issue
Block a user