pythonfunctionfirst-class

How to package a sequence functions that act on parameter in order in Python


Imagine there are three functions, all them accept and return the same type args.

Normally, we can write it as fun3(fun2(fun1(args)), this can be say that a sequence function act on parameter in order, which likes one variety Higher-order functions "map".

You know in Mathematica, we can write this as fun3@fun2@fun1@args.

Now the question is that can we integrate fun3@fun2@fun1 as another fun without modifying their definition, so fun(args) can replace fun3(fun2(fun1(args)), this looks more elegant and concise.


Solution

  • def merge_steps(*fun_list):
        def fun(arg):
            result = arg
            for f in fun_list:
                result = f(result)
            return result
    
        return fun
    
    
    def plus_one(arg):
        return arg + 1
    
    
    def double_it(arg):
        return arg ** 2
    
    
    def power_ten(arg):
        return arg ** 10
    
    
    combine1 = merge_steps(power_ten, plus_one, double_it)
    combine2 = merge_steps(plus_one, power_ten, double_it)
    
    combine1(3) 
    > 3486902500
    

    or use lambda:

    steps = [power_ten, plus_one, double_it]
    
    reduce(lambda a, f: f(a), steps, 3)
    > 3486902500