python中的函数流水线操作
pipe-fn的Python项目详细描述
安装
pip install pipe-fn
管道fn
纯粹的函数转换。
frompipe_fnimporte# e is the identity mappingdefadd(this,other):returnthis+otherprint([1,2,3]|e/sum)# sum([1, 2, 3])# => 6print([2,3,-1]|e**{'key':lambdax:-x}/sorted)# set kwargs# sorted([2, 3, -1], key=lambda x: -x)# => [3, 2, -1]print([[1],[2],[3]]|e/sum*([],))# set args# sum([[1], [2], [3]], [])# => [1, 2, 3]defmy_func(self,*args,**kwargs):returnself,args,kwargsprint([1,2,3]|e**dict(a=1,b=2,c=3)# you should set kwargs first because of the high priority of `**` operator.*(4,5,6)# it'okay to change the order of setting `args` and setting `function`./my_func)# => ([1, 2, 3], (4, 5, 6), {'a': 1, 'b': 2, 'c': 3})print(1|e/add*(1,))# add(1, 1)# => 2defdouble(x):return2*x# and then compositionprint(1|(e/add*(2,)+double+double))# double(double(add(1, 2))# => 12 = (1 + 2) * 2 * 2# set single arg[['a'],['b']]|e/sum@[] \ |e/print# print(sum([['a'], ['b']], []))# => [1, 2]defdouble(x):return2*x[1,2,3]|e/std.general.Sum@double \ |e/print# print(Sum([1,2,3], double))# => 12