What is this functional “pattern” called?

2019-04-08 09:25发布

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.

7条回答
倾城 Initia
2楼-- · 2019-04-08 09:44

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

查看更多
贼婆χ
3楼-- · 2019-04-08 09:44

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.

查看更多
forever°为你锁心
4楼-- · 2019-04-08 09:45

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楼-- · 2019-04-08 09:46

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?

查看更多
放荡不羁爱自由
6楼-- · 2019-04-08 09:48

A delegate?

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

Didn't understand, sorry...

查看更多
地球回转人心会变
7楼-- · 2019-04-08 09:55

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

查看更多
登录 后发表回答