views:

493

answers:

7

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.

A: 

A delegate?

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

Didn't understand, sorry...

Ironicnet
This is not your usual C# thing. Functional programming is a different world altogether where functions return functions
Frederick
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
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
+5  A: 

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
+5  A: 

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

unbeknown
+8  A: 
Jonathan Tran
This seems to be what's going on, something like "toThunk"
Robert Gould
It's not really a thunk in a call-by-value language (is it?) since no computation would be delayed.
Chris Conway
+2  A: 

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.

unbeknown
+1  A: 

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
+1  A: 

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