Page 1 of 1

What is a lambda expression?

Posted: Tue Oct 09, 2012 2:46 am
by RichAlgeni
According to:
http://www.lambdafaq.org/what-is-a-lambda-expression/
In mathematics and computing generally, a lambda expression is a function: for some or all combinations of input values it specifies an output value. Lambda expressions in Java introduce the idea of functions into the language. In conventional Java terms lambdas can be understood as a kind of method with a more compact syntax that allows declarations to omit modifiers, the method identifier, the return type, and in some cases the parameter types.
I'm someone who does not get 'all excited' over object orientation. It's not that I have anything against it, I just don't have a lot for it. I would classify it in the same category as scrum or agile, more sizzle than steak. Could the same thing be said for lambda expressions, or am I missing something?

Re: What is a lambda expression?

Posted: Tue Oct 09, 2012 5:36 am
by DarkDragon
A lambda expression is a short form of function declarations. Think of the following pseudocode:

Code: Select all

int SortCompare(int x, int y) {
  return Math.signum(x-y);
}

void AnyFunc() {
  Collection<int> collectionInput;
  // .. fill the collection with some random unsorted numbers
  Collection<int> collectionResult;
  
  collectionResult = collectionInput.Sort(SortCompare);
}
The same, but with lambda expressions:

Code: Select all

void AnyFunc() {
  Collection<int> collectionInput;
  // .. fill the collection with some random unsorted numbers
  Collection<int> collectionResult;
  
  collectionResult = collectionInput.Sort((x, y) => Math.signum(x-y));
}

Re: What is a lambda expression?

Posted: Tue Oct 09, 2012 2:55 pm
by Shield
Lambda expressions are especially useful for short function definitions or "callbacks",
like DD showed. It's mostly syntactic sugar, but it is useful as you can access variables
of the outlining functions. I use those expressions quite often.

Re: What is a lambda expression?

Posted: Tue Oct 09, 2012 4:51 pm
by RichAlgeni
Thanks Daniel.

Shield, do you use them in PB, or are you talking C?

Re: What is a lambda expression?

Posted: Tue Oct 09, 2012 5:10 pm
by Shield
They don't exist in PB. I use them in C#.