FTP下载程序(包括ascii和binary)
Guess language of text using ZIP

让人晕的一条函数式编程的代码

dcy posted @ 2009年9月05日 23:29 in Python with tags python , 4335 阅读

今天在python俱乐部的python过程式和函数式编程那里看了很久,还是没搞明白它的具体过程,我没办法了,还是贴出来

 

Python 过程式编程与函数式编程

过程式与函数式是两种截然不同的编程方式和思考方法,下面以求解素数为例做一下对比。

采用过程式编程

def isPrime(n):
 mid = int(pow(n,0.5)+1)
 for i in xrange(2,mid):
 if n % i == 0 : return False
 return True
 
primes=[]
for i in xrange(2,1000):
 if isPrime(i): primes += [i]
 print primes

采用函数式编程

print reduce(lambda l,y:not 0 in map(lambda x:y % x, l) and l+[y] or l,xrange(2,1000), [] )

它同上面的算法是一样的,想看懂的话必须先知道map、reduce的用法,参考Python的官方文档,提示一下:l表示已经找到的素数序列,not 0 in map(lambda x:y %x,l) 表示数y能否被l中的任何一个数整除,继而返回l+[y]或者l。
对比一下这两段程序,可以明显地看出过程式的代码虽长但直白,适合初学算法的人,而函数式的代码短而晦涩,有着数学一样的抽象,适合hacker。但是如 果你习惯了函数编程的思维方式,反而会觉得代码直观明了。不管采用哪种方式的编程,代码的可读性都是非常重要的,要根据具体的场合选用合适的编程方式。
从效率的角度讲,一般函数式编程的效率会低一些。比如上面的例子,在确定一个数是不是质数时,过程式只要找到了一个它的因数就返回,而函数式需要除以比它小的所有质数,计算量要多一些。

 

PT 说:
2009年9月20日 23:35

py2.5之后,用x if oo else y的写法更加pythonic:

print reduce(lambda l,y:l+[y] if not 0 in map(lambda x:y % x, l) else l, xrange(2,1000), [] )

zaiya 说:
2023年1月14日 11:26

There's something about functional programming code that can make even the most experienced developers dizzy. Perhaps it's the lack of mutable state or the strict adherence to pure functions that can make it seem like a foreign language. CBD Bioavailability But whatever the reason, there's no denying that functional programming code can be pretty daunting.

Emma 说:
2023年1月21日 19:18

Accidentally I have come across this website and little bit confused about the real estate property Lompoc details shared here. I have gone through the title shared here and that is about The last pit before NOI. I am looking here to more details regarding that and hope that you will share more details over here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter