views:

160

answers:

7

Why not write the anonymous function content only instead of the anonymous function AND the anonymous function content?

A: 

It is there so that you don't have to create a function wrapper that is essentially useless.

Mitch Wheat
+7  A: 

Because you don't write an anonymous function then immediately call it. It's usually passed to some other function that does things with it (e.g. uses the anonymous function to map an array to another array).

Tom Dalling
+1  A: 

Probably the biggest use is for one-off functions that won't be used anywhere else or need to be created on demand, dynamically. That way, they don't need to "pollute" the namespace.

For example, in python:

x_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_values = map(lambda x: x**x + 2x + 3, x_values)

By using the lambda, I don't need to create a function in the module namespace just for one polynomial (x^2 + 2x + 3).

There are many areas where they come in handy, especially in functional programming (lookup closures or currying, for example).

ars
A: 

They exist because there are lots of times you want to have method semantics with expression syntax.

var l = new List<int>() { 1, 2, 3, 4, 5 };
var a = l.Where( i => i > 3 );  // a = 4, 5

I didn't want to bother writing a new method predicate that checked if an integer was greater than three. I want the meaning of a method where I can only put an expression. The anonymous function lets me have both. Woot! :)

JP Alioto
A: 

If you are asking "Why use a lambda expression?", then there are a few answers. For one, lambda expressions, being shorthand, are usually easier to read and understand than the much more verbose alternatives of full anonymous functions (i.e. with the delegate() term), or a full blown function and delegate. Lambda expressions are also ideal for performing short, simple, "expressive" computations (i.e. simple mathematics on a set of input), which is quite frequent in software programming.

Lambda expressions are a powerful tool that can help improve the expressiveness, simplicity, and readability of your code.

jrista
+2  A: 

They're commonly used for callbacks. You can't just write the content, because your code doesn't execute it directly.

patros
If not directly, when is it executed? It doesn't even have a name!
Delirium tremens
It is executed whenever the function you pass it to decides, just like if you passed it a named function. As for it not having a name, so what? If you malloc memory in C it doesn't have a name, just an address. An anonymous function is the same. Under the hood you're usually creating a function somewhere on the stack, and really just passing a pointer to that function.
patros
A: 

I don't know if this even applies, since it is specific to Javascript and I don't see a Javascript tag, but....it's not uncommon to create+call an anonymous function at once. It's often done in the sake of preserving the global scope from variable pollution.

var x = 1;
(function(){        
    var x = 2;
})();
x == 1;
ken