What is lambda and what is an example implementation?

I am new to programming, and while reading a lot this concept lambda continues to rise, but it's hard for me to find out what it really is and how its implementation will make my programming life so much better. So firstly, what is lambda and secondly, how can it be implemented?

Thanks to everyone who posted. As mentioned in the comments, this is a duplicate, but there are so many great answers here that I want to keep them for the community, so I turn it into a community message. Here is a link to another question:

What is lambda (function)?

+6
lambda
source share
6 answers

It is hard to catch a lambda, but once you have portrayed them, you cannot understand why you did not do this before.

Lamdba - Anonymous Functions

Lambda are ordinary functions, the only difference is that you are not giving them a name.

To understand this, you must first know that when creating a function, the code is stored in memory at an address known only to the computer.

So, when you do something like this:

function Foo () { /* your code here */ } 

What you really do is bind the name "Foo" to the code address in memory.

Now there is another way to access the address: links (and pointers, but skip these nasty guys)

Well, a lambda function is a function that has no name, so access can only be with its link.

How do you use them?

When you create a lambda function, you usually plan to use it only once.

The step-by-step process is usually:

  • Create function
  • Get the link
  • Pass the link somewhere, it will be used

Finally, the link is lost, and therefore the function is automatically destroyed.

A typical use case is a callback function. You declare, create and pass a function in one line, therefore it is convenient.

Real word example

In Python, you can use lambda in lists:

 /* create a list of functions */ function_list = [(lambda x : number_to_add + x) for number_to_add in range(0, 10) ] 

In Javascript, you usually pass a function to other functions. JQuery example:

  $("img").each( /* here we pass a function without any name to the "each()" method */ function(i){ his.src = "test" i ".jpg"; } ); 

What you better know

  • Some languages, such as Javascript or Lisp, make extensive use of lambdas. It may be culturally reasonable, but the functional programming paradigm tends to lead to lambda mania.

  • Long lambdas make code difficult to read. This is why some languages ​​limit the capabilities of lambdas, such as Python, which prevent them from claiming "if".

  • Lambda is just normal functions. Wherever you use, you can use the regular function. This is just a coding style question.

+12
source share

Lambda is a built-in function description. It originated from functional programming languages, and the number of other languages ​​that support something like this is growing today. The name comes from a mathematical thing called the lambda calculus, which has influenced functional programming languages ​​(like Lisp), and the idea of ​​lambda comes from it.

Your question depends on the programming language you are talking about. For example, in F # you should use fun x -> x * x to represent

 int myfunction(int x) { return x * x; } 

In C #, you should use x => x * x to represent the same function. How it is used and what you can do with it largely depends on the language you work in.

Speaking of C #, the great thing about them is the ability to parse them as expression trees . A lambda expression can be used as code, for example, delegate (roughly a function pointer) or as data in an expression tree. Using them as expression trees, create libraries such as LINQ to SQL to use the expression to create an SQL statement to send to the server and get the appropriate results.

+9
source share

Lambda is a tool for creating an anonymous function or closing. In imperative languages ​​(and functional), this is equivalent to resolving nested functions, where the internal function has access to the local variable and the parameters of the attached function. It is in functional languages ​​with the keywords lambda , fun , fn or even \ ; in Smalltalk, this is called a block. It is also found in most scripting languages, such as Perl, Python, Lua, etc.

About single languages without lambda

  • Languages ​​without nested functions like Standard C or Icon

  • Languages ​​with secondary nested functions --- a function cannot be returned from a function, stored in a global variable, or stored in a data structure distributed by a heap. This language family includes Pascal and his descendants in the families Modula, Ada and CLU.

Lambda has important implications for programmers and compiler authors: it is no longer possible to store all local variables on the stack . Instead, some variables can be captured and stored in a closed heap. Keep in mind that when you write lambda, you write selection .

Example: one of the simplest functions is composition (Haskell syntax):

 compose fg = \x -> f (gx) 

This suggests that compose takes two functions f and g as arguments and returns an anonymous function that takes its argument x and then applies g and then f - x . The compose application creates a closure allocated on the heap that stores the value f and g , as well as a pointer to the code for the lambda body. In languages ​​where lambda is common, such as Haskell, ML, Caml, and Scheme, a lot of effort has been put into ensuring a quick distribution of placement . Some scripting languages, such as Lua, have unusual implementations that make non-lambda code the same as in the imperative language, and also make lambda fast enough. Lambda also runs fast on Smalltalk, which was also designed to allocate many objects on the heap . In languages ​​where lambda has been upgraded, such as Perl or Java (inner classes are associated with lambda), costs can be relatively higher.

In general, if the language was designed with lambda in mind, you can use them as much as you want. Especially in ML, Caml, Scheme, Haskell, even anonymous functions - cheap cheap --- use a lot of them!

+6
source share

lambda in the programming world means an anonymous function that can be passed and returned like any other normal variable. The so-called functional languages ​​are built into them, but recently the number of languages ​​supporting them has been growing, since they allow you to write reusable code. See This, for example, in the next version of C ++:

 // write this once... int transform_values(int * values, int n, function<int(int)> f) { for(int i = 0; i < n; i++) values[i] = f(values[i]); } int values[] = { 1, 2, 3, 4 }; // ... then call it to double the values in an array transform_values(values, 4, [](int v) { return v * 2; }); 

It is similar to C # and other languages ​​that support lambdas. Now there is the word "closure". This means that lambda can capture local variables and use them when calculating its result:

 int local_variable = 5; int values[] = { 1, 2, 3, 4 }; // ... then call it to multiply the values in an array transform_values(values, 4, [=](int v) { return v * local_variable; }); 

The local_variable variable local_variable now fixed inside the closure and can be used inside it. Variables can also be updated by closing. Lambdas is the main building block of functional languages. Here is an example in haskell:

 map (\x -> x * 2) [1, 2, 3, 4] 

Will do the same as the C ++ code above. It displays the values ​​in the list using this function (lambda here) in the list of results. Using haskell, you can see perfectly how the syntax is used, comparing the mathematical concept of Lambda Calculus .

+5
source share

Lambda lambda calculus , but I think people use it with the term closure interchangeably. See What is a closure?

The Ruby implementation is easy to understand and very powerful. In the following times code, the method takes a block of code between curly braces and calls it HEIGHT times. You can define a similar method that takes a block of code and implements cyclic constructive things.

 @cells = [] HEIGHT.times { @cells << empty_row } 

I think it is more interesting if there is a parameter.

 5.times { |i| puts i, " " } 
+2
source share

Lambda means different things in different languages. I know about them in the context of python, but I heard that python has a different approach than other languages.

Essentially, in python, lambda is an anonymous function that can consist of only one expression, the result of which is returned.

I understand that in other languages ​​they are more generalized anonymous functions without restricting one expression, but I'm not sure about the details.

An anonymous function is what it looks like. Function without a name. For example, they are often used as event handlers or in any case when you need a simple callback function, but don’t want to clutter up the namespace.

+2
source share

All Articles