from functools import partial
ret = None
class pipe:
def __init__(self, data):
self.data = data
def __or__(self, right):
if right is ret:
return self.data
elif isinstance(right, tuple):
return pipe(partial(*right)(self.data))
else:
return pipe(right(self.data))
# with partial function
pipe(10) | range | partial(map, lambda x: x ** 2) | list | print
# simplify partial function
pipe(10) | range | (map, lambda x: x ** 2) | list | print
# assign return value to x
x = pipe(10) | range | (map, lambda x: x ** 2) | list | ret
另外两个成果:
1
Sequencer 2017-01-21 01:46:50 +08:00 via iPhone 2
想像写 python 一样写 shell 才是需求啊...
|
2
czheo OP @Sequencer Just for fun. 再来一个例子:
··· from itertools import groupby {k: list(v) for k, v in pipe([4,3,1,3,4,2,1,9]) | sorted | groupby | ret} ## {1: [1, 1], 2: [2], 3: [3, 3], 4: [4, 4], 9: [9]} ··· |
4
freestyle 2017-01-21 19:36:57 +08:00
重载 or 操作符 可以可以
|
6
franklinyu 2017-01-22 16:09:46 +08:00
@itfanr 你說的 shellpy 能改環境變量麼?
|
7
itfanr 2017-01-22 22:48:47 +08:00 via Android
@franklinyu 自己试试呗
|