What is a lambda expression?

Everything else that doesn't fall into one of the other PB categories.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

What is a lambda expression?

Post 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?
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: What is a lambda expression?

Post 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));
}
bye,
Daniel
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: What is a lambda expression?

Post 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.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: What is a lambda expression?

Post by RichAlgeni »

Thanks Daniel.

Shield, do you use them in PB, or are you talking C?
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: What is a lambda expression?

Post by Shield »

They don't exist in PB. I use them in C#.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Post Reply