Differences between revisions 15 and 16
 ⇤ ← Revision 15 as of 2005-08-08 19:20:12 → Size: 4251 Editor: hoxide Comment: ← Revision 16 as of 2005-08-08 19:26:33 → ⇥ Size: 5509 Editor: hoxide Comment: Deletions are marked like this. Additions are marked like this. Line 125: Line 125: == reduce ==reduce( function, sequence[, initializer]) Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned. == filter ==filter( function, list) Construct a list from those elements of list for which function returns true. list may be either a sequence, a container which supports iteration, or an iterator, If list is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of list that are false (zero or empty) are removed. Note that filter(function, list) is equivalent to [item for item in list if function(item)] if function is not None and [item for item in list if item] if function is None.

# 前言

• 本文的写作计划源于2004年9月 GreyRoar 的提议, 可惜真正动手却推到了现在, 在近一年的时间里, python的函数编程也发生了一些改变, python3000计划和最近的已经出现的"匿名块"等都使得python在这个方面的发展充满着不确定因素, 本文只讨论目前已知的能用的功能, 不保证满足将来可能发生的变化. 另外在一切开始前, 记住Python和数学里一样记号只是记号.

# 函数式编程的概念

• 函数编程源自 Lisp 等高阶语言, 他们以λ演算为基础. 所谓λ演算其实指基于算子λ的一个演算体系. 他的特殊性在于, 存在一个算子 λ (λ只是记号, 没有更多含义), 他的作用是将运算式子变成函数 . 例如: λx: x+1 就将 x+1这个运算式子, 变成了关于x的一个函数, 他的值是x+1. 光有λ算子构成不了完整的λ运算体系, 还需要一些辅助算子, 但是λ演算体系的基础就是这个算子. 要完全了解这个体系则需要一环扣一环的定义和定理, 太复杂这里不再详述(书丢学校了, 回校有时间补齐 :P ).

# python中的函数编程

## lambda

• 和数学中的 λ 一样, python的函数编程体系中有一个 lambda 语句, 用来生成函数, 一般这样的函数称为匿名函数.

```   1 lambda x: x+1
```

```   1 >>> lambda x: x+1 # 生成一个匿名函数 λ(x) = x+1
2 <function <lambda> at 0x00C99770>
3 >>> f = lambda x: x+1 # 将这个函数绑定名字 'f'
4 >>> f(1) # 调用 'f'
5 2
```

lambda的具体语法是:

lambda [parameter_list]: expression

parameter_list 是参数表, expression 是后面跟的表达式, lambda本身是个运算符, 作用在这两个元素上产生一个匿名函数, 类似于以下函数定义:

```   1 def name(parameter_list):
2     return expression
```

expression 是一个合法的python表达式, 显然 expression 中的变量量除了在 parameter_list 中的外必须是已知的.

## 辅助算符

• 所谓的辅助算符在python中其实是一组很有用的函数.

### map

• map是最基本的函数, 搞懂了map其他函数就很容易搞懂. map 的定义: map( function, list, ...) 将函数function作用到 list 的每个元素上, 将结果组成一个列表返回. 如果参数列表有多个, 函数应该是有多参数的, 每个分量取各列表上的对应值, 如果有列表比其他列表短, 不足部分当作 None . 这里list可以是任意序列(sequence), 例如列表(list) , 元组(tuple)等.

```   1 >>> a = range(5);
2 >>> b = range(4);
3 >>> map(lambda x, y: (x, y) , a, b) # lambda x,y 接受两个参数, 生成个包含这两个参数的tuple.
4 [(0, 0), (1, 1), (2, 2), (3, 3), (4, None)]
```

• map(lambda (arg1, ..., argn): expression, multiple_list), multiple_list 是一个列表, 他的元素还是一个列表, 即参数列表.

```   1 >>> ml = [[0, 1], [2, 3], [3, 4]]
2 >>> map(lambda (x,y): x+y , ml) # lambda (x,y): x+y 接受一个有两元元组(tuple), 计算他们的和.
3 [1, 5, 7]
```

```   1 >>> a = range(3)
2 >>> map(None , a)
3 [0, 1, 2]
```

## reduce

reduce( function, sequence[, initializer])

Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

## filter

filter( function, list)

Construct a list from those elements of list for which function returns true. list may be either a sequence, a container which supports iteration, or an iterator, If list is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of list that are false (zero or empty) are removed. Note that filter(function, list) is equivalent to [item for item in list if function(item)] if function is not None and [item for item in list if item] if function is None.

# 交流

• 已经开始了？强烈支持呀，呵呵——GreyRoar

• ["FP编程的一点资料"] 有了系统的转载, 先并人（入） ["FLOSS"] -- Py Tips 了是也乎! -- Zoomq

fp4python (last edited 2009-12-25 07:15:25 by localhost)