Is it a good idea to have a syntax sugar to function composition in Python?

后端 未结 4 687
我寻月下人不归
我寻月下人不归 2021-02-05 08:37

Some time ago I looked over Haskell docs and found it\'s functional composition operator really nice. So I\'ve implemented this tiny decorator:

from functools im         


        
4条回答
  •  广开言路
    2021-02-05 09:14

    You can do it with reduce, although the order of calls is left-to-right only:

    def f1(a):
        return a+1
    
    def f2(a):
        return a+10
    
    def f3(a):
        return a+100
    
    def call(a,f):
        return f(a)
    
    
    reduce(call, (f1, f2, f3), 5)
    # 5 -> f1 -> f2 -> f3 -> 116
    reduce(call, ((lambda x: x+3), abs), 2)
    # 5
    

提交回复
热议问题