撰写功能和功能模块

max*_*max 13 python functional-programming function function-composition python-3.x

Python 3.2文档是指Collin Winter的functional模块,其中包含以下功能compose:

compose()函数实现了函数组合.换句话说,它返回外部和内部callables周围的包装器,这样内部的返回值直接送到外部.

不幸的是,这个模块自2006年7月以来一直没有更新; 我想知道是否有任何更换.

现在,我只需要compose功能.以下原始functional.compose定义是否仍适用于Python 3?

def compose(func_1, func_2, unpack=False):
    """
    compose(func_1, func_2, unpack=False) -> function

    The function returned by compose is a composition of func_1 and func_2.
    That is, compose(func_1, func_2)(5) == func_1(func_2(5))
    """
    if not callable(func_1):
        raise TypeError("First argument to compose must be callable")
    if not callable(func_2):
        raise TypeError("Second argument to compose must be callable")

    if unpack:
        def composition(*args, **kwargs):
            return func_1(*func_2(*args, **kwargs))
    else:
        def composition(*args, **kwargs):
            return func_1(func_2(*args, **kwargs))
    return composition
Run Code Online (Sandbox Code Playgroud)

这个问题有点相关; 它询问Python是否应支持特殊语法compose.

Kru*_*Kru 6

您的实现compose对python 3.2有效,如上面的注释中所述.您提供的库的大多数函数都有一个写在文档中的python等价物.

函数如mapfilter已经在python中实现,也可以简单地表达为列表推导.Python有一个id函数返回一个对象的身份(作为整数),但id库的功能可以表示为lambda x: x.

你可能会觉得有趣的另一个模块是itertoolsfunctools哪个(partialreduce类似foldl但参数顺序不一样).

以下是我在标准库中找不到的一些简单实现:

from functools import reduce

def flip(f):
    if not callable(f):
        raise TypeError("Cannot filp a non-callable object")
    def result(*args, **kw):
        args = list(args)
        args.reverse()
        return f(*args, **kw)
    return result

def ilast(i):
    return reduce(lambda _, x: x, i)

def iscanl(f, v, seq):
    yield v
    for a in seq:
        v = f(v, a)
        yield v

def scanl(*args, **kw):
    return list(iscanl(*args, **kw))

def foldl(*args, **kw):
    return ilast(iscanl(*args, **kw))
# Or using reduce
#def foldl(f, v, seq):
#    return reduce(f, seq, v)

def iscanr_reverse(f, v, seq):
    return iscanl(flip(f), v, seq)

def scanr(*args, **kw):
    result = list(iscanr_reverse(*args, **kw))
    result.reverse()
    return result

def foldr(*args, **kw):
    return ilast(iscanr_reverse(*args, **kw))
Run Code Online (Sandbox Code Playgroud)