Lambda functions

I'm really curious how lambda functions are used. Does it make sense to use them in a modern, high-level programming language such as php? If so, why?

Is it really just a function built into the function ( like this ), or is there something behind?

+4
source share
5 answers

A lambda member is a function as a value. It can be passed in exactly the same way as a value (as it is one), and yet it is applied, giving it some arguments. Named functions are simply names that map to lambda terms, either as constants or variables (depending on the details of the language).

And yes, they are quite common in high-level programming languages. For example, all functions in JavaScript are actually lambda terms stored in variables.

+7
source

A lambda function is an object (or value). It can be passed as an argument to another function or returned from one.

map(lambda x: x**2, [1,2,3,4]) 

He does not need a name; when you create it (e.g. in Python) lambda x: x**2 , you have the lambda function right there. Compare this to the more ordinary:

 def square(x): return x**2 

Perhaps most importantly, you can create them at runtime.

 def make_exp_function(n): return lambda x: x**n square = make_exp_function(2) cube = make_exp_function(3) for i in range(100): f = make_exp_function(i) print f(2) 
+5
source

Although PHP supports anonymous functions, they are not actually closed - so you will miss the most interesting part.

In JavaScript (and in languages ​​that support "lambdas" "The correct path"), you can return a function from a function, and the return can access the enclosing context even after the external function completes. Example:

 function counter(start){ return function() { return start++; }; }; var f = counter(1); alert( f() ); // now it 1 alert( f() ); // now it 2 alert( f() ); // now it 3 var g = counter(10); alert( g() ); // now it 10 alert( g() ); // now it 11 alert( f() ); // now it 4! 
+5
source

There are two important things about lambda functions (at least as the term is commonly used):

  • They can be circumvented. A function can take a lambda function as an argument or return a lambda function.

  • They have access to the area in which they are defined. So, for example, the lambda function can access the arguments to the closing function.

For a basic example of how this is useful, I will write a filter function in Ruby:

 def filter(array, &lambda_function) new_array = [] for item in array new_array.push(item) if lambda_function[item] end return new_array end $people_array = #imagine I have an array of Person objects here def find_people_richer_than(threshold) return filter($people_array) {|person| person.wealth >= threshold} end 

Our filter does not know what criteria we will use for filtering. This logic is contained in the block (Ruby version of the lambda function) that we pass. But note that although the function of the lambda function is actually called inside the filter , it still knows the threshold from where it was defined.

+3
source

Basically this is syntactic sugar, what you write in 10 lines with delegates and what you have can be done in a single line lambda expression. The main thing to take into account me is that when working with several developers in one project, you must make sure that its readability is still clear.

You can add an event inline Button1_Click += (sender, eventArgs) => { //myCode };

But you cannot reuse the event, and if there is a lot of code there, it will not help keep your code transparent.

You can also create a lambda to retrieve data from the <> list, but it can become unclear when there are many options where a good LINQ query can be much clearer.

This is basically a personal choice, but I have to say that I have used it quite a few times :)

Hope this helps a bit, Sam

+2
source

Source: https://habr.com/ru/post/1311382/


All Articles