6

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.

Chris Conway
  • 55,321
  • 43
  • 129
  • 155
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
  • I don't think I'd call it a pattern, any more than if statements are a pattern. A pattern usually solves a specific problem. This is just a pretty fundamental language construct. You just have a higher-order function that returns a closure. – jalf Jan 14 '09 at 13:01
  • What's the context? And how much of this code are we talking about? Is it just that the inner function is able to access variables defined in the outer? That's just a closure. Or is it that you're able to return nested functions? That's just a property of higher-order functions. – jalf Jan 14 '09 at 13:08

7 Answers7

11

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

Jonathan Tran
  • 15,214
  • 9
  • 60
  • 67
5

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

4

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

Remo.D
  • 16,122
  • 6
  • 43
  • 74
2

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.

1

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?

Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
  • Yes I think the pattern (micro-pattern?)'constantly' is the right name for this. I didn't actually have any purpose for it, but when I was creating a functional library, as a learning experience, I muddled my way into the possibility of this and noticed it was kind of special and might have a proper name – Robert Gould May 14 '10 at 09:04
1

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.

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
-2

A delegate?

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

Didn't understand, sorry...

Ironicnet
  • 547
  • 3
  • 14
  • This is not your usual C# thing. Functional programming is a different world altogether where functions return functions – G S Jan 14 '09 at 12:34
  • Frederick is right, its not the usual stuff, but I don't know enough either to place a name on what I'm doing, although it seems like a useful thing to do – Robert Gould Jan 14 '09 at 12:36
  • i think i need to start learning to read the tags before answering... didn't know it was related to functional programming... so i assumed my language. – Ironicnet Jan 14 '09 at 13:15