Size: 5514
Comment: fp4python 重新开动, 先写到这里, 明天继续, 有什么不对的或不好的地方大家一起改阿.
|
Size: 5471
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 77: | Line 77: |
== 辅助算符 == 所谓的辅助算符在python中其实是一组很有用的函数. |
== 高阶函数 == 所谓的高阶函数其实是一组以函数为参量的函数. 下面介绍 Python 中的具体形式. |
Line 127: | Line 127: |
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. | 将函数function从左到右作用在序列的每个元素上, 函数function为二元函数, 第一个变量为上次计算所得结果,第二个变量为列表元素. 具体得: {{{ #!python >>> reduce(lambda x,y: x+y , [1,2,3,4,5]) # 相当于 ((((1+2)+3)+4)+5) 15 }}} 整个计算过程就像列表在不断缩减一样. initializer为初值. 例如: {{{ >>> reduce(lambda x,y: x+y , [1,2,3,4,5], 10) # 相当于(((((10+1)+2)+3)+4)+5) 25 }}} |
Line 132: | Line 148: |
filter 过滤器. 构造一个列表 |
函数编程, 任何人可以以任何方式随意转载. 清剿太监贴~~ , 把没完成的东西完成之
-- hoxide [DateTime(2004-09-08T23:06:48Z)] TableOfContents
前言
本文的写作计划源于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
- 就生成了一个函数 λ(x) = x+1. 具有可以在python的解释器中试试:
- lambda的具体语法是: lambda [parameter_list]: expression parameter_list 是参数表, expression 是后面跟的表达式, lambda本身是个运算符, 作用在这两个元素上产生一个匿名函数, 类似于以下函数定义:
- expression 是一个合法的python表达式, 显然 expression 中的变量量除了在 parameter_list 中的外必须是已知的. 注意experssion中是不能用print语句的, 要想产生输出的话可以用sys.stdout.write()等函数. 下面会不断给出 lambda 的例子
高阶函数
- 所谓的高阶函数其实是一组以函数为参量的函数. 下面介绍 Python 中的具体形式.
map
- map是最基本的函数, 搞懂了map其他函数就很容易搞懂. map 的定义: map( function, list, ...) 将函数function作用到 list 的每个元素上, 将结果组成一个列表返回. 如果参数列表有多个, 函数应该是有多参数的, 每个分量取各列表上的对应值, 如果有列表比其他列表短, 不足部分当作 None . 这里list可以是任意序列(sequence), 例如列表(list) , 元组(tuple)等. 例如:
- 另一种多参数调用的方法是组合列表: map(lambda (arg1, ..., argn): expression, multiple_list), multiple_list 是一个列表, 他的元素还是一个列表, 即参数列表. 这种方法实质还是单参数调用(这个不一定要完全理解). 例如:
- 如果map 的第一个参数, 即function等于None, 则默认为恒等变换. 例:
reduce
reduce( function, sequence[, initializer])
将函数function从左到右作用在序列的每个元素上, 函数function为二元函数, 第一个变量为上次计算所得结果,第二个变量为列表元素. 具体得:
整个计算过程就像列表在不断缩减一样.
initializer为初值. 例如:
>>> reduce(lambda x,y: x+y , [1,2,3,4,5], 10) # 相当于(((((10+1)+2)+3)+4)+5) 25
filter
filter( function, list)
filter 过滤器.
构造一个列表 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