What is this functional “pattern” called?

徘徊边缘 提交于 2019-12-21 12:37:33

问题


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.


回答1:


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...

function WhatAmIDoing2(args...)
   return function()
       return ackermann(args)
   end
end

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.)




回答2:


In functional programming a function that takes another function as an argument or returns another function is called a higher-order function.




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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:

(map (constantly 9) [1 2 3])
=> (9 9 9) 

Just wondering, what do you use this for?




回答7:


A delegate?

Basically you are returning a function?? or the output of a function?

Didn't understand, sorry...



来源:https://stackoverflow.com/questions/442772/what-is-this-functional-pattern-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!