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