这种符号代码转换叫什么?
我经常会遇到这种代码变换(甚至是数学变换)。 (Python 示例,但适用于任何语言。)
我已经将一个函数
def f(x):
return x
用在另一个函数中。
def g(x):
return f(x)*f(x)
print g(2)
导致 4
但我想删除函数依赖性,我将函数 g 更改为
def g(f):
return f*f
print g( f(2) )
导致 4
你如何调用这种转换,在本地将函数转换为标量?
I often cross this kind of code transformation (or even mathematical transformation). (Python example, but applies to any language.)
I've go a function
def f(x):
return x
I use it into another one.
def g(x):
return f(x)*f(x)
print g(2)
leads to 4
But I want to remove the functional dependency, and I change the function g into
def g(f):
return f*f
print g( f(2) )
leads to 4 too
How do you call this kind of transformation, locally turning a function into a scalar ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否有一个特定的术语。
一般来说,对于函数式编程,传递标量参数和将函数作为参数传递之间通常没有区别。
在第一个示例中,我仍然可以调用
g(f(2))
并且它应该计算f(f(2))*f(f(2))
,其中(因为f(x)
是恒等变换)也将得到 4 作为答案。I'm not sure there is a specific term for it.
In general terms for functional programming there usually isn't a distinction made between passing scalar arguments and passing functions as arguments.
In the first example I could still call
g(f(2))
and it should calculatef(f(2))*f(f(2))
, which (sincef(x)
is the identity transformation) will also result in 4 as the answer.