How to multiply functions in python?

我怕爱的太早我们不能终老 提交于 2019-11-27 20:26:25
Jazzer

You can use your hack class as a decorator pretty much as it's written, though you'd likely want to choose a more appropriate name for the class.

Like this:

class Composable(object):
    def __init__(self, function):
        self.function = function
    def __call__(self, *args, **kwargs):
        return self.function(*args, **kwargs)
    def __mul__(self, other):
        @Composable
        def composed(*args, **kwargs):
            return self.function(other(*args, **kwargs))
        return composed
    def __rmul__(self, other):
        @Composable
        def composed(*args, **kwargs):
            return other(self.function(*args, **kwargs))
        return composed

You can then decorate your functions like so:

@Composable
def sub3(n):
    return n - 3

@Composable
def square(n):
    return n * n

And compose them like so:

(square * sub3)(n)

Basically it's the same thing you've accomplished using your hack class, but using it as a decorator.

Python does not (and likely will never) have support for function composition either at the syntactic level or as a standard library function. There are various 3rd party modules (such as functional) that provide a higher-order function that implements function composition.

Maybe something like this:

class Composition(object):
    def __init__(self, *args):
        self.functions = args

    def __call__(self, arg):
        result = arg
        for f in reversed(self.functions):
            result = f(result)

        return result

And then:

sorted(my_list, key=Composition(square, sub3))

You can compose functions using SSPipe library:

from sspipe import p, px

sub3 = px - 3
square = px * px
composed = sub3 | square
print(5 | composed)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!