这个功能“模式”是什么? 叫?
当我遇到这个函数的需要时,我正在闲逛一些函数式编程,但是我不知道这种东西在标准术语中被称为什么。 有人认得吗?
function WhatAmIDoing(args...)
return function()
return args
end
end
编辑:概括了该函数,它需要可变数量的参数(或者可能是隐式列表)并返回一个函数,该函数在调用时返回所有参数,类似于咖喱或泡菜,但它没有似乎也不是。
I was fooling around with some functional programming when I came across the need for this function, however I don't know what this sort of thing is called in standard nomenclature.
Anyone recognizes it?
function WhatAmIDoing(args...)
return function()
return args
end
end
Edit: generalized the function, it takes a variable amount of arguments ( or perhaps an implicit list) and returns a function that when invoked returns all the args, something like a curry or pickle, but it doesn't seem to be either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
WhatAmIDoing 是一个高阶函数,因为它是一个返回另一个函数的函数。
它返回的是一个 thunk - 为延迟计算实际值而创建的闭包。 通常创建 thunk 是为了延迟计算表达式(并可能记住它),但在其他情况下,只需要一个函数来代替裸值,如“
constantly 5
”的情况,在某些语言中,它返回一个始终返回 5 的函数。后者可能适用于给定的示例,因为假设该语言按应用顺序求值(即在调用函数之前求值参数),则该函数除了将值到返回它们的函数中。
WhatAmIDoing 实际上是我所描述的“持续”功能的实现。 但一般来说,您不必在内部函数中仅返回
args
。 您可以返回“ackermann(args)
”,这可能需要很长时间,例如...但是 WhatAmIDoing2 会立即返回,因为 ackermann 函数的计算将在 关闭。 (是的,即使是按值调用的语言也是如此。)
WhatAmIDoing is a higher-order function because it is a function that returns another function.
The thing that it returns is a thunk — a closure created for delayed computation of the actual value. Usually thunks are created to lazily evaluate an expression (and possibly memoize it), but in other cases, a function is simply needed in place of a bare value, as in the case of "
constantly 5
", which in some languages returns a function that always returns 5.The latter might apply in the example given, because assuming the language evaluates in applicative-order (i.e. evaluates arguments before calling a function), the function serves no other purpose than to turn the values into a function that returns them.
WhatAmIDoing is really an implementation of the "constantly" function I was describing. But in general, you don't have to return just
args
in the inner function. You could return "ackermann(args)
", which could take a long time, as in...But WhatAmIDoing2 would return immediately because evaluation of the ackermann function would be suspended in a closure. (Yes, even in a call-by-value language.)
在函数式编程中,将另一个函数作为参数或返回另一个函数的函数称为高阶函数< /a>.
In functional programming a function that takes another function as an argument or returns another function is called a higher-order function.
我想说 XXXX 返回绑定在 x、y 和 z 值上的未命名函数的闭包。
这篇维基百科文章可能会提供一些启示
I would say that XXXX returns a closure of the unnamed function bound on the values of x,y and z.
This wikipedia article may shed some light
柯里化是将一个函数转换为一系列函数,每个函数只接受一个参数并返回另一个参数这样的功能。 所以,这个例子与柯里化无关。
Pickling 是一个通常用于表示某种序列化的术语。 也许用于存储由多个值构建的对象。
如果您感兴趣的方面是返回的函数可以访问 XXXX 函数的参数,那么我会选择 Remo.D。
Currying is about transforming a function to a chain of functions, each taking only one parameter and returning another such function. So, this example has no relation to currying.
Pickling is a term ususally used to denote some kind of serialization. Maybe for storing a object built from multiple values.
If the aspect interesting to you is that the returned function can access the arguments of the XXXX function, then I would go with Remo.D.
正如其他人所说,它是一个高阶函数。 由于您的问题中有“模式”,我想我应该补充一点,函数式语言的这一功能通常是使用 没有高阶函数的语言中的策略模式。
As others have said, it's a higher-order function. As you have "pattern" in your question, I thought I'd add that this feature of functional languages is often modelled using the strategy pattern in languages without higher-order functions.
在 Clojure 中,非常类似的东西被称为
constantly
:http://github.com/richhickey/clojure/blob/ab6fc90d56bfb3b969ed84058e1b3a4b30faa400/src/clj/clojure/core.clj#L1096
仅
不断
返回的函数接受任意数量的参数,使其比您的模式更通用(且灵活)。我不知道这个模式是否有名称,但在需要正常函数的情况下会使用它,但我关心的是返回某个值:
只是想知道,你用它做什么?
Something very similar is called
constantly
in Clojure:http://github.com/richhickey/clojure/blob/ab6fc90d56bfb3b969ed84058e1b3a4b30faa400/src/clj/clojure/core.clj#L1096
Only the function that
constantly
returns takes an arbitrary amount of arguments, making it more general (and flexible) than your pattern.I don't know if this pattern has a name, but would use it in cases where normally functions are expected, but all I care for is that a certain value is returned:
Just wondering, what do you use this for?
代表?
基本上你正在返回一个函数? 或者函数的输出?
没看懂,抱歉...
A delegate?
Basically you are returning a function?? or the output of a function?
Didn't understand, sorry...