Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use anonymous function? [duplicate]

Possible Duplicate:
How do you use anonymous functions in PHP?

Why should i use an anonymous function? I mean, what's the real deal using it? I just don't really get this. I mean, you use function to make the code more clean or to use it more than once. But Anonymous functions just don't do neither the first nor the second. I googled them and i couldn't find anyone asking the same problem.

like image 469
Shoe Avatar asked Nov 10 '10 17:11

Shoe


People also ask

What are the advantages of using an anonymous function?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.

When would you use an anonymous function?

Anonymous functions are often arguments being passed to higher-order functions, or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function.

Why do we use anonymous function in JavaScript?

An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. 3. This function is useful for all scenarios. An anonymous function can be useful for creating IIFE(Immediately Invoked Function Expression).

What are two common uses of anonymous functions in JavaScript?

One common use for anonymous functions is as arguments to other functions. Another common use is as a closure, for which see also the Closures chapter. Use as an argument to other functions: setTimeout(function() { alert('hello'); }, 1000);


1 Answers

I would say that anonymous functions show their beauty when there is good library classes/functions that use them. They are not that sexy by themselves. In the world of .net there is technology called LINQ that makes huge use of then in very idiomatic manner. Now back to PHP.

First example, sort:

uasort($array, function($a, $b) { return($a > $b); }); 

You can specify complex logic for sorting:

uasort($array, function($a, $b) { return($a->Age > $b->Age); }); 

Another example:

$data = array(          array('id' => 1, 'name' => 'Bob', 'position' => 'Clerk'),          array('id' => 2, 'name' => 'Alan', 'position' => 'Manager'),          array('id' => 3, 'name' => 'James', 'position' => 'Director')  );   $names = array_map(          function($person) { return $person['name']; },          $data  ); 

You see how nicely you can produce array of names.

Last one:

array_reduce(    array_filter($array, function($val) { return $val % 2 == 0; },    function($reduced, $value) { return $reduced*$value; } ) 

It calculates product of even numbers.

Some philosophy. What is function? A unit of functionality that can be invoked and unit of code reuse. Sometimes you need only the first part: ability to invoke and do actions, but you don't want to reuse it at all and even make it visible to other parts of code. That's what anonymous functions essentially do.

like image 130
Andrey Avatar answered Oct 05 '22 14:10

Andrey