Re: [比較] 我為何鍾情於用 Scala 做為兵刃(三)

作者: COLDTURNIP (娶妻當娶五航戰)   2011-01-22 02:54:57
既然 Scala 的例子用了 lambda function,
小弟也來幫忙改寫一下 Python 的部份;
如果上面的 lambda function 其實只會用到一次的話也不用特地宣告之,
list comprehension 可以直接解決,亦不失其可讀性:
==== <Example_1>
xs = [-1, -2, -3, 0, 1, 2, 3]
print len([x for x in xs if x**2 > 5])
### equivalent to:
# square = lambda x: x**2
# isGreaterThan5 = lambda x: x > 5
# print len([x for x in xs if isGreaterThan5(square(x))])
==== </Example_1>
作者: kaluvchris (Juanito)   2011-01-23 14:59:00
說 讚!

Links booklink

Contact Us: admin [ a t ] ucptt.com