Functional pipes in python like %>% from R's magritrr

后端 未结 14 2224
春和景丽
春和景丽 2020-11-29 16:17

In R (thanks to magritrr) you can now perform operations with a more functional piping syntax via %>%. This means that instead of coding this: <

14条回答
  •  星月不相逢
    2020-11-29 16:50

    I missed the |> pipe operator from Elixir so I created a simple function decorator (~ 50 lines of code) that reinterprets the >> Python right shift operator as a very Elixir-like pipe at compile time using the ast library and compile/exec:

    from pipeop import pipes
    
    def add3(a, b, c):
        return a + b + c
    
    def times(a, b):
        return a * b
    
    @pipes
    def calc()
        print 1 >> add3(2, 3) >> times(4)  # prints 24
    

    All it's doing is rewriting a >> b(...) as b(a, ...).

    https://pypi.org/project/pipeop/

    https://github.com/robinhilliard/pipes

提交回复
热议问题