小编chi*_*123的帖子

在 python 中将两个函数通过管道传输到第三个二进制函数

我正在慢慢尝试使用Python进行函数式编程,并遇到了以下问题:

给定两个函数f1和f2,我如何构造一个函数 f ,以“以函数方式”将这两个函数与相同的参数相乘?

没有深入研究函数式编程,我确实有一个解决方案

f = lambda x : f1(x) * f2(x)
Run Code Online (Sandbox Code Playgroud)

但它似乎不符合函数式编程的正确精神。

我的下一次尝试是使用像这样的mul和运算符juxt

>>> from tools import juxt
>>> from operator import mul
>>> f = mul(juxt(f1,f2))
TypeError: op_mul expected 2 arguments, got 1
Run Code Online (Sandbox Code Playgroud)

尝试拆分juxtwith的元组输出*也不起作用:

>>> f = mul(*juxt(f1, f2))
TypeError: mul() argument after * must be an iterable, not juxt
Run Code Online (Sandbox Code Playgroud)

再次使用lambda似乎有效,但不知何故它破坏了整个目的......

>>> temp  = juxt(f_1, f_2)
>>> f = lambda x : …
Run Code Online (Sandbox Code Playgroud)

python functional-programming operators toolz

4
推荐指数
1
解决办法
528
查看次数

标签 统计

functional-programming ×1

operators ×1

python ×1

toolz ×1