I guess I'm a bit shaky on what anonymous functions really are then. You can use python functions to do the same sort of thing:
def dosomething(item):
print i
map(dosomething, listOfNumbers)
Well, that's more like the function pointers I was talking about in C, or maybe delegates in C#. Anonymous functions in the simplest sense are functions that aren't bound to any name, but instead are passed around by "value", or probably more correctly by reference. As I said, anonymous functions don't ultimately allow you to do anything different, they just make it easier to do the same things.
Well,
All functions (I think) in python are first class, so you can define them within other functions and also pass them around like any other object, and I can't see much else you'd need. Of course I've never really used anonymous functions so I really wouldn't know
Well, I don't think they could qualify as fully first-class because you cannot create full new functions at run-time. If a languages treats functions as first-class objects then that means you are able to create new functions at run-time without any restrictions (Pythons anonymous functions have restrictions, so they aren't completely first-class), i.e., you can even pass them as arguments return them as values.
function saysSomething(something) {
return function() {
alert(something);
}
}
var func = saysSomething("Hello");
func();
In this specific case, we have a function that returns another functions. The functions "saysSomething" creates and returns another function (at runtime) which will, when called, print out the the screen whatever you originally passed to saysSomething, which in this case is "Hello".
However, if Python allows you to
declare functions within other functions, then this sort of thing might be possible. The returned function here is called a "closure", because it is evaluated within the scope of the function that it was created in. Closures can be very useful. Or maybe it's just that without closures, anonymous functions would be less useful...
Another example would be a button object.
new Button(function() {
alert("Hey there!");
});
new Button(function() {
alert("Hello World!");
});
This illustrates how anonymous functions can be particularly useful when used in event based system, such as GUIs. In this example, when the user clicked the button, the function passed as an argument would be called. Now, it would be entirely possible to produce the same results
without anonymous functions, but it would be arguably messier. This is only two buttons, but some GUI applications have hundreds of buttons and many other even more complex controls.
function onClick() {
alert("Hey there!");
}
new Button(onClick);
// Already have a function called "onClick", so we need to come up with a new name...
function onClick2() {
alert("Hello World!");
}
new Button(onClick2);
function onClick3 { ...
That's another way of doing it with still using callback methods to handle events, but without using anonymous functions. It's not as clean, and will get even messier as the project gets bigger.
Another way to accomplish the same thing is the OO method.
class Button {
virtual function onClick();
}
class Button1 inherits Button {
function onClick() {
alert("Hey there!");
}
}
class Button2 inherits Button {
function onClick() {
alert("Hello World!");
}
}
The object oriented way of doing the same thing (rather,
one of the OO ways) is also fairly clean, but is definitely more verbose than the anonymous function way. In C++ a common convention is storing class definitions in separate header files, which doubles the amount of files in your project making it harder to maintain.
Yet
another way (last one
) to accomplish the same thing:
function onClick(button) {
switch (button) {
case "button1":
alert("Hey There!");
break;
case "button2":
alert("Hello World!");
break;
}
}
new Button(onClick, "button1");
new Button(onClick, "button2");
This last way is a fairly common way of accomplish event based architectures. It's clean and simple, and doesn't require too much code. However, a problem with this method is that you'll find yourself spending a lot of time jumping back and forth between places in your project since all the controls are created in one place, and their behaviors are defined in another. This could actually be considered either a good thing or a bad thing, depending on how you want to organize your code. Another problem with this method is that it tends to create very large functions, since lots of different behaviors are handled in a smaller amount of functions. The other two methods are better in this respect because each behavior gets its own function.
So there you have it. Three ways of doing the same thing thing, each with the same end, just different means of achieve that end. If your project is small, the means usually doesn't really matter. However, as the scope of your project gets bigger, it generally gets exponentially more complex
unless your start creating your code on a higher level. Languages like C or C++ have a harder time doing this because they are static, fairly strict, and inherently low level languages, so you have to do a lot more initial work to achieve the high levels of programming that you desire. If you do a good enough job then you don't really need anonymous functions, but it's usually not a trivial matter to "do a good enough job" when it comes to simulating high level concepts in a low level language. The results are usually slightly less than ideal, which still gets the job done, but requires more pain, headaches and hacks than one would feel comfortable dealing with on a regular basis (these problems are amplified or reduced depending on how much of a purist you are.
)
With higher level languages, you get most (if not all) of the higher level constructs right out of the box, reducing total amount initial of work needed by the programmers by a large amount. That means, in general, there's less "distance" (i.e., lines of code), between where you are, and where you want to be, which, when it comes to coding, is always a good thing.
Hehe, I feel like I just wrote an essay.
I kinda got a little side tracked (well, maybe just too abstract), and I don't even know if that answered your question. But maybe now you have a better idea of why anonymous functions might be useful.