更好用的 Python 管道!

2021-04-19 15:46:18 +08:00
 abersheeran

之前的主题 https://v2ex.com/t/743574

吸取了大家的建议,做了一个更好用的管道出来。根据测试,性能损耗在纳秒级,我个人觉得应该可以忽略。

还是老样子,不多说,直接看代码。仓库在 https://github.com/abersheeran/cool

from functools import reduce
from cool import F, FF

range(10) | F(filter, lambda x: x % 2) | F(sum) == 25

(1, 2) | FF(lambda x, y: x + y) == 3

range(10) | F(reduce, lambda x, y: x + y) == 45
range(10) | F(reduce, lambda x, y: x + y, ..., 10) == 55

square = F(pow, ..., 2)
range(10) | F(map, square) | F(sum) == 285
1993 次点击
所在节点    Python
5 条回复
SjwNo1
2021-04-19 16:18:42 +08:00
cool
... 没太懂,表示占位吗
abersheeran
2021-04-19 16:23:42 +08:00
@SjwNo1 是的。在仓库 README 里有解释的。
hanyceZ
2021-04-20 13:32:01 +08:00
给大佬点个赞
listenerri
2021-04-20 18:01:42 +08:00
不明觉厉,点个赞
abersheeran
2021-04-20 18:20:57 +08:00
@listenerri 用就完事!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/771685

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX