Python中更好的函数组成 [英] Better Function Composition in Python
本文介绍了Python中更好的函数组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Python工作。最近,我发现了一个精彩的小包,名为 fn 。我一直在使用它的功能组成。
I work in Python. Recently, I discovered a wonderful little package called fn. I've been using it for function composition.
例如,而不是:
baz(bar(foo(x))))
使用fn,您可以写:
(F() >> foo >> bar >> baz)(x) .
当我看到这个,我立刻想到了Clojure:
When I saw this, I immediately thought of Clojure:
(-> x foo bar baz) .
但是请注意,在Clojure中,输入在左边。我不知道这是否可能在python / fn。
But notice how, in Clojure, the input is on the left. I wonder if this possible in python/fn.
推荐答案
你不能复制确切的语法, :
You can't replicate the exact syntax, but you can make something similar:
def f(*args):
result = args[0]
for func in args[1:]:
result = func(result)
return result
似乎有效:
>>> f('a test', reversed, sorted, ''.join)
' aestt'
这篇关于Python中更好的函数组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文