Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda Expressions

Tags:

c

lambda

Can somebody explain me lambda expressions & what they can be used for. I have googled for it & have a rough idea. most of the examples give c# code. How about lambda expressions in plain old C...?

like image 614
Asad Khan Avatar asked Apr 22 '10 21:04

Asad Khan


People also ask

What is a lambda expression example?

Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable).

What are lambda expressions used for?

Lambda expression is an anonymous function that provides a very concise and functional syntax which is further used for writing anonymous methods. The programming of a function is a body concept, and it's used for creating expression tree types or delegates.

What is the type of lambda expression?

Lambda Expressions are anonymous functions. These functions do not need a name or a class to be used. Lambda expressions are added in Java 8. Lambda expressions basically express instances of functional interfaces An interface with a single abstract method is called a functional interface.

How is lambda expression represented?

Programming Lambda Expressions in JavaThe arrow operator is used to represent lambda expressions in Java. Here is an example that shows this: (x) -> x * x; The left side of the arrow operator is used to specify the parameters.


2 Answers

There are actually two things called "lambda expressions", which are rather loosely related:

  1. Lambda expressions are fundamental part of lambda calculus and are closely related to functional programming

  2. In imperative languages, lambda expressions are usually synonyms for anonymous methods. In C#, for example you can pass lambda expression (ie. an expression itself, not just its result) as an argument:

C#:

someCollection.Apply (x => 2*x); // apply expression to every object in collection
// equivalent to 
someCollection.Apply (delegate (int x) { return 2 * X; });

Having said that, C does not support anonymous methods. You can, however, use function pointers to achieve similar results:

int multiply (int x)
{
    return 2 * x;
}

...
collection_apply (some_collection, multiply);
like image 147
el.pescado - нет войне Avatar answered Oct 17 '22 22:10

el.pescado - нет войне


el.pescado's answer is right but the example that he provides has an easy work around, using a function pointer. Many uses of lambda functions can't be solved with c's function pointers.

Say you write these functions in c:

int Multiply_1(int x) { return(x*1); }
int Multiply_2(int x) { return(x*2); }
int Multiply_3(int x) { return(x*3); }
int Multiply_4(int x) { return(x*4); }
etcetera, to infinity

Those are all pretty easy to understand. Now assume that you want to write a function that takes y as input and returns a pointer to the function Multiply_y():

(int)(int) *Make_Multiplier(int y) { return(Multiply_y); }

Where "Multiply_y" is a dynamically created function of the form of Multiply_1, Multiply_2, etc. Languages that have first-class lambda functions can do that.

like image 39
Eyal Avatar answered Oct 17 '22 21:10

Eyal