Python函数式编程

函数式编程,编程范式--实现在某个函数中调用其他函数。

仅支持有限的函数式编程:

filter(func,seq):调用一个布尔函数func来遍历每个seq中的元素,返回一个使func返回值为true的元                           素的序列。

In [154]: def f1(x):
.....:     if x > 20 : return True
.....:     else:
.....:         return False

In [155]: filter(f1,l1)
Out[155]: [55, 66, 77, 23]

 

map(func,seq1[,seq2...]):将函数func作用于给定序列(s)的每个元素,并用一个列表来提供返回值;如果func为None,func表现为一个身份函数,返回一个含有每个序列中元素集合的n个元组的列表。---映射器  将不同列表上的同一索引的元素进行运算后的返回值整合成为一个元组。

In [158]: l1 = [0,1,2,3,4,5,6]

In [159]: l2 = ['Sun','M','W','T','F','S']

In [160]: map(None,l1,l2)
Out[160]: [(0, 'Sun'), (1, 'M'), (2, 'W'), (3, 'T'), (4, 'F'), (5, 'S'), (6, None)]

 

In [163]: def f2(x):
.....: return x * 2
.....:

In [164]: map(f2,l1,l2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-164-dc39bd58c3d7> in <module>()
----> 1 map(f2,l1,l2)

TypeError: f2() takes exactly 1 argument (2 given)

In [165]: map(f2,l1,l2)
KeyboardInterrupt

In [165]: def f3(*x):
.....:     return x * 2
.....:

In [166]: map(f3,l1,l2)
Out[166]:
[(0, 'Sun', 0, 'Sun'),
(1, 'M', 1, 'M'),
(2, 'Tue', 2, 'Tue'),
(3, 'W', 3, 'W'),
(4, 'T', 4, 'T'),
(5, 'F', 5, 'F'),
(6, 'S', 6, 'S')]

 


In [167]: def f4(x,y):
.....:     return x * 2, y * 2
.....:

In [168]: map(f4,l1,l2)
Out[168]:
[(0, 'SunSun'),
(2, 'MM'),
(4, 'TueTue'),
(6, 'WW'),
(8, 'TT'),
(10, 'FF'),
(12, 'SS')]

 

reduce(func,seq[,init])  :将二元函数作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列元素),连续的将现有结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值;如果初始值init 给定,第一个比较会是init和第一个序列元素,而不是序列的头两个元素。--- 折叠

In [169]: def f5(x,y):
.....:     return x + y
.....:

In [170]: reduce(f5,l1)
Out[170]: 21

In [171]: reduce(f5,l1,10)
Out[171]: 31

 

Python函数闭包及装饰器

In [172]: def f1(x):
.....:     def f2(y):
.....:         return y **x
.....:         return f2
.....:

In [174]: f1(4)
Out[174]: <function __main__.f2>

In [175]: f3 = f1(3)


In [179]: type(f3)
Out[179]: function

In [180]: f3
Out[180]: <function __main__.f2>

In [181]: f3(2)
Out[181]: 8

In [182]: f3(3)
Out[182]: 27

内层函数可以记忆调用的外层函数的变量。--Python的闭包--lexical closure --词法闭包

In [191]: def startPos(m,n):
def NewPos(x,y):
print "The ole position is (%d, %d), and the new position is (%d, %d)." % (m,n,m+x,n+y)
return NewPos
.....:

In [192]: pos = startPos(10,10)

In [193]: type(pos)
Out[193]: function

In [194]: pos(1,2)
The ole position is (10, 10), and the new position is (11, 12).

In [195]: pos(-1,3)
The ole position is (10, 10), and the new position is (9, 13).

 

可用使用list函数将一个生成器转换成列表。

 

yield生成器

In [196]: def genNum(x):
.....:     y = 0
.....:     while y <= x:
.....:         yield y
.....:         y += 1
.....:

In [197]: g1 = genNum(10)

In [198]: type(g1)
Out[198]: generator

In [199]: g1.next()
Out[199]: 0

In [200]: g1.next()
Out[200]: 1

In [201]: g1.next()
Out[201]: 2

In [202]: g1.next()
Out[202]: 3

In [203]: g1.next()
Out[203]: 4

 

 

In [205]: def genNum(n):
    i = 1
    while i <= n:
        yield i ** 2
        i += 1
.....:

In [206]: g1 = genNum(20)

In [207]: for i in g1:
.....:     print i
.....:
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400

 

函数中使用yield,会返回一个生成器对象。

 

装饰器:自身是一个函数,用于装饰其他函数。增强被装饰函数的功能。实现函数代码重用,函数功               能在不同环境中重用。

一般接受一个函数对象作为参数,以对其进行增强。

In [208]: def deco(func):
.....:     def wrapper():
.....:         print "Please say something: "
.....:         func()
.....:         print "No zuo no die...."
.....: return wrapper
.....:

In [209]: @deco
.....: def show():
.....: return "I am from Mars."
.....:

 

In [212]: @deco
def show():
print "I am from Mars."
.....:

In [213]: show()
Please say something:
I am from Mars.
No zuo no die....

 

 

In [214]: def deco(func):
.....:     def wrapper(x):
.....:         print "Please say something...>"
.....:         func(x)
.....:         print "OK"
.....:     return wrapper
.....:

In [215]: @deco
.....: def show(x):
.....:     print x
.....:

In [216]: show('hello world')
Please say something...>
hello world
OK

 

posted on 2015-12-21 23:35  雲斷丶  阅读(73)  评论(0)    收藏  举报

导航