组成功能和功能模块 [英] compose function and functional module
问题描述
Python 3.2文档指的是 Collin Winter的功能
模块,其中包含函数 compose
:
compose()函数实现函数组合。在其他
字词中,它会返回一个包裹外部和内部可调用对象的包装,例如
,从内部返回的值将直接传递给outer。
不幸的是,这个模块自2006年7月以来一直没有更新;现在,我只需要 compose
函数。现在,我只需要 compose
函数。下面的原始 functional.compose
定义对于Python 3还是很好的吗?
func_1,func_2,unpack = False):
compose(func_1,func_2,unpack = False) - >函数
返回的函数
也就是说,compose(func_1,func_2)(5)== func_1(func_2(5))
如果不可调用(func_1 ):
raise TypeError(第一个参数组合必须可调用)
如果不可调用(func_2):
raise TypeError(第二个参数组合必须可调用)
如果解包:
def组合(* args,** kwargs):
返回func_1(* func_2(* args,** kwargs))
else:
def组合(* args,** kwargs):
返回func_1(func_2(* args,** kwargs))
返回结构
问题是有点相关;它询问Python是否应该支持 compose
的特殊语法。 compose
的实现对python 3.2有效,正如上面的注释中所讨论的那样。
您提供的库的大部分功能都有一个与文档中写入的python等价物。
函数,如 map
和过滤器
已经在python中实现,并且也可以简单地表示为列表解析。 Python有一个返回对象身份的 id
函数(作为整数),但库的 id
函数可以可以表示为 lambda x:x
。
另一个您可能感兴趣的模块是 这是一个在标准库中找不到其中的一些简单实现: Python 3.2 documentation refers to Collin Winter's The compose() function implements function composition. In other
words, it returns a wrapper around the outer and inner callables, such
that the return value from inner is fed directly to outer. Unfortunately, this module hasn't been updated since July 2006; I wonder if there's any replacement available. For now, I only need This SO question is somewhat related; it asks whether Python should support special syntax for Your implementation of Functions such as Another modules you might find interesting are Here is a simple implementations of a few of them that I didn't find in the standard library:
这篇关于组成功能和功能模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! itertools 和
functools
其中部分
和减少
(类似于 foldl
,但参数顺序不一样)。
from functools import减少
def flip(f):
如果不可调用(f):
raise TypeError(Can not filp a non-callable object)
def result(* args,** kw):
args = list(args)
args.reverse()
返回f(* args,** kw)
返回结果
def ilast(i):
return reduce(lambda _,x:x,i)
def iscanl(f,v,seq):
yield v
for a se se:
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使用reduce
#def foldl(f,v,seq):
#return reduce(f,seq,v)
$ b $ def iscanr_reverse(f,v,seq):
返回iscanl(flip(f),v,seq)
def scanr(* args,** kw):
result = list(iscanr_reverse(* args,** kw ))
result.reverse()
返回结果
$ b $ def def foldr(* args,** kw):
return ilast(iscanr_reverse(* args,** kw))
functional
module which contains function compose
:
compose
function. Is the following original functional.compose
definition still good for 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
compose
.compose
is valid for python 3.2 as discussed in the comments above.
Most of the functions of the library you gave have a python equivalent written in the documentation.map
and filter
are already implemented in python and can also be simply expressed as list comprehensions. Python has an id
function returning the identity of an object (as integer), but the id
function of the library can be expressed as lambda x: x
.itertools
and functools
which has partial
and reduce
(which is similar to foldl
but the argument order is not the same).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))