1

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.

3 Answers 3

2
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
Sign up to request clarification or add additional context in comments.

1 Comment

really cool , it is my ideal ,especially that the combine of lambda and reduce suits me, it is a chip off the mathematica " f@@expr or Apply[f,expr] " like f2@f1@t gives f2[f1[t]] , without define a function . Thanks
1

I think you can use Function Recursion in python to do this.

def function(args, times):
    print(f"{times} Times - {args}")
    if times > 0 : 
        function(args,times - 1)

function("test", 2)

Note: I just add times argument to not generate infinite loop.

1 Comment

it gives me that it goes far.
1

I'm not certain I understand your question, but are you talking about function composition along these lines?

# Some single-argument functions to experiment with.

def double(x):
    return 2 * x

def reciprocal(x):
    return 1 / x

# Returns a new function that will execute multiple single-argument functions in order.

def compose(*funcs):
    def g(x):
        for f in funcs:
            x = f(x)
        return x
    return g

# Demo.

double_recip_abs = compose(double, reciprocal, abs)
print(double_recip_abs(-2))   # 0.25
print(double_recip_abs(.1))   # 5.0

3 Comments

it works also, but it doesn't seperate the new function generation and its parameter call , 'compose' returns the final result , not the new function。
@eason OK, now I think I understand your goal. See edited answer.
absolutely, the use of closures, it hit me, but I am an introductory python er.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.