如何应用一个函数n次?

时间:2022-02-06 07:10:54

I have been trying without much success to implement a higher order function that would take in repeat(f, n) where f is another function and n is the integer value that says how many times n will be applied to a variable x. For example:

我一直在尝试实现一个更高阶的函数,它将采用重复(f, n),其中f是另一个函数,n是整数值,表示一个变量x将被应用多少次n。

def integer(x):

      x + 1

so i would have repeat (integer, 5) and I would have integer(integer(integer(integer(integer(x)

所以我要重复(integer, 5)还有integer(integer(integer(integer(integer(integer(integer, integer)))

4 个解决方案

#1


4  

You can use a simple for loop.

您可以使用一个简单的for循环。

>>> def integer_inc(x):
...     return x + 1
... 
>>> def repeat(func, n, x):
...     for i in range(n):
...         x = func(x)
...     return x
... 
>>> repeat(integer_inc, 5, 1)
6
>>>

#2


4  

Well, there's the iterative approach:

有一种迭代方法:

def repeat(f, n):
    def new_fn(x):
        for _ in range(n):
            x = f(x)
        return x
    return new_fn

higher_fn = repeat(lambda x: x+3, 5)

higher_fn(2)    # => 17 == (((((2 + 3) + 3) + 3) + 3) + 3)

and the compositional approach:

和成分的方法:

def repeat(f, n):
    new_fn_str = "lambda x: {}x{}".format("f(" * n, ")" * n)
    new_fn     = eval(new_fn_str, {"f": f})
    return new_fn

which results in exactly the same thing but may be slightly faster.

结果是完全相同的,但可能会稍微快一点。

#3


3  

You can create a decorator,

你可以创建一个decorator,

from functools import wraps


def repeat(n=1):
    def decorator(func):
        @wraps(func)
        def wrapper(args):
            args = func(args)
            for i in xrange(n-1):
                args = func(args)
            return args
        return wrapper
    return decorator

if __name__ == "__main__":
    @repeat(n=6)
    def test(a):
        print a
        return a+1
    test(1)

#4


3  

We also have the recursive implementation:

我们还有递归实现:

def repeat(f, n):
    if n == 1:
        return f
    else:
        return lambda x: f(repeat(f,n-1)(x))

#1


4  

You can use a simple for loop.

您可以使用一个简单的for循环。

>>> def integer_inc(x):
...     return x + 1
... 
>>> def repeat(func, n, x):
...     for i in range(n):
...         x = func(x)
...     return x
... 
>>> repeat(integer_inc, 5, 1)
6
>>>

#2


4  

Well, there's the iterative approach:

有一种迭代方法:

def repeat(f, n):
    def new_fn(x):
        for _ in range(n):
            x = f(x)
        return x
    return new_fn

higher_fn = repeat(lambda x: x+3, 5)

higher_fn(2)    # => 17 == (((((2 + 3) + 3) + 3) + 3) + 3)

and the compositional approach:

和成分的方法:

def repeat(f, n):
    new_fn_str = "lambda x: {}x{}".format("f(" * n, ")" * n)
    new_fn     = eval(new_fn_str, {"f": f})
    return new_fn

which results in exactly the same thing but may be slightly faster.

结果是完全相同的,但可能会稍微快一点。

#3


3  

You can create a decorator,

你可以创建一个decorator,

from functools import wraps


def repeat(n=1):
    def decorator(func):
        @wraps(func)
        def wrapper(args):
            args = func(args)
            for i in xrange(n-1):
                args = func(args)
            return args
        return wrapper
    return decorator

if __name__ == "__main__":
    @repeat(n=6)
    def test(a):
        print a
        return a+1
    test(1)

#4


3  

We also have the recursive implementation:

我们还有递归实现:

def repeat(f, n):
    if n == 1:
        return f
    else:
        return lambda x: f(repeat(f,n-1)(x))