This repository has been archived by the owner on Jan 31, 2021. It is now read-only.
Filter/Map/Reduce 的便捷绑定 #1
abersheeran
started this conversation in
Ideas
Replies: 4 comments 24 replies
-
from functools import reduce
from pipe import F
Filter = F(F, filter)
Map = F(F, map)
Reduce = F(F, reduce) e.g. range(100) | Filter(lambda x: x % 2) | Map(lambda x: x * x) | Reduce(lambda x, y: x + y) |
Beta Was this translation helpful? Give feedback.
23 replies
-
我看了下cpython源代码,好像默认的reduce是用C实现的,所以initial 不能作为keyword参数, |
Beta Was this translation helpful? Give feedback.
0 replies
-
试了两种写法,确实是LZ这种写法舒服一些。 item = PIPE | range(10) | (map , str) | list | END
item = range(10) | Map(str) | list 1L这套简易实现的问题在于没法链式输出,比如 item = range(100) | Filter(lambda x: x % 2) | Map(lambda x: x * x) | list # 这种用法会报错 因为Map直接返回map对象,左右两边都缺魔术方法。要实现直接输出还是要自己实现对象接管逻辑,就不似这么轻了。 我做了一个简易实现:https://github.com/GoodManWEN/pipeit,可以实现直接输出或返回迭代器。 >>> from pipeit import *
>>> for _ in range(10) | Map(str): ...
>>> src = list((list(range(_,_+3)) for _ in range(0,12,3)))
>>> src
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
>>> src | Map(lambda x:x[0]) | list
[0, 3, 6, 9]
>>> list(range(6)) | Reduce(lambda x , y : x + y , 25)
40 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
e.g.
Beta Was this translation helpful? Give feedback.
All reactions