From ce13fd44e8427d64513eee47c2c346deec4979c8 Mon Sep 17 00:00:00 2001 From: pezy Date: Wed, 19 Apr 2017 18:26:44 +0800 Subject: [PATCH] init higherOrderFunctions --- note_cn/README.md | 2 + note_cn/higherOrderFunctions.md | 90 +++++++++++++++++++++++ note_cn/reference&value.md | 7 ++ practice_projects/characterPictureGrid.py | 14 ++++ practice_projects/commaConnection.py | 14 ++++ practice_questions/ch04/README.md | 0 6 files changed, 127 insertions(+) create mode 100644 note_cn/higherOrderFunctions.md create mode 100644 note_cn/reference&value.md create mode 100644 practice_projects/characterPictureGrid.py create mode 100644 practice_projects/commaConnection.py create mode 100644 practice_questions/ch04/README.md diff --git a/note_cn/README.md b/note_cn/README.md index 2bd8249..1c6fe9b 100644 --- a/note_cn/README.md +++ b/note_cn/README.md @@ -1,2 +1,4 @@ # 中文笔记 +- [引用和值](reference&value.md) +- [高阶函数专题](higherOrderFunctions.md) \ No newline at end of file diff --git a/note_cn/higherOrderFunctions.md b/note_cn/higherOrderFunctions.md new file mode 100644 index 0000000..61c8258 --- /dev/null +++ b/note_cn/higherOrderFunctions.md @@ -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 +``` + +接着, 需求又变了, 让你计算下图所示形式的和: + +![](http://composingprograms.com/img/pi_sum.png) + +这是一种慢速求解 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 (n): + total, k = 0, 1 + while k <= n: + total, k = total + (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 +``` + diff --git a/note_cn/reference&value.md b/note_cn/reference&value.md new file mode 100644 index 0000000..7f33e45 --- /dev/null +++ b/note_cn/reference&value.md @@ -0,0 +1,7 @@ +# 引用与值 + +几乎在任何语言里, 这两者的区分都是组最基础的重点. + +Python 里的区别不露痕迹, 对于 `x = y` 这样的式子, x 到底是引用, 还是值, 完全取决于 y 的类型. 如果 y 是字符串, 数值, 元组这种**不可变类型**, x 就是值. 而如果 y 是列表, 字典这样的**可变类型**而言, x 就是引用. + +对于列表而言, 如果你想要 copy 值, 就需要显式的调用 `copy()` 方法, 如果要更彻底一些, 连 list 里包含的子 list 也要一起 copy, 那么则需要用到 `deepcopy()` 方法. \ No newline at end of file diff --git a/practice_projects/characterPictureGrid.py b/practice_projects/characterPictureGrid.py new file mode 100644 index 0000000..d8ba3d1 --- /dev/null +++ b/practice_projects/characterPictureGrid.py @@ -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() \ No newline at end of file diff --git a/practice_projects/commaConnection.py b/practice_projects/commaConnection.py new file mode 100644 index 0000000..40b0fe7 --- /dev/null +++ b/practice_projects/commaConnection.py @@ -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)) \ No newline at end of file diff --git a/practice_questions/ch04/README.md b/practice_questions/ch04/README.md new file mode 100644 index 0000000..e69de29