Jump to content



Photo

Can someone explain where Lambda Expressions would be useful?

Answered Go to the full post

  • Please log in to reply
10 replies to this topic

#1 winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 27 June 2013 - 20:26

I was wondering what the benefits of using Lambda Expressions are. Can someone think of examples of where they would be beneficial to use? Let's say that I have a website where I want to add and manipulate customer and product information from a database. Is that a good place to use them for instance? Thank you in advance!



Best Answer Andre S. , 27 June 2013 - 23:38

Lambdas are more than inline functions, they are also closures. Any lambda that captures local variables, fields, etc, is compiled not to a method but to a type. C++ programmers are familiar with this concept under the name of functors; it was the only way to do closures before the advent of lambda expressions in C++11. C# had support for closures before lambdas actually, in C# 2.0 there were already anonymous methods which are basically lambdas with an ugly syntax. With lambdas, LINQ and the Func and Action types, C# 3.0 made functional programming much easier.

Go to the full post



#2 Xilo

Xilo

    Neowinian Senior

  • Joined: 28-May 04
  • Location: Austin, TX

Posted 27 June 2013 - 20:30

Lambda expressions are good for one off items that require a function but not necessarily need a full blown function created.

 

One use could be finding an object within a list that contains a certain value for one of it's member attributes.



#3 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 27 June 2013 - 20:38

Ok, so it would be good for searching for information / generating a report on one specific product basically?



#4 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 27 June 2013 - 20:49

Yes, see the wikipedia entry for some good examples: http://en.wikipedia....amming)#Sorting

 

If you're referring to them in the more general sense of being anonymous functions, then they get thrown all over in Javascript.



#5 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 27 June 2013 - 20:51

Ok, I will take a look. Thank you!



#6 francescob

francescob

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 04-November 08

Posted 27 June 2013 - 20:54

Ok, so it would be good for searching for information / generating a report on one specific product basically?

 

Just like for anonymous functions it's just to avoid flooding classes with methods with the most basic functionalities.



#7 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 27 June 2013 - 20:59

Oh ok! That makes it a lot clearer! I can definitely see now how using Lambdas would be useful instead of having tons of small methods all over the place. That is exactly what I was looking for (the reason they were created). Thank you!



#8 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 27 June 2013 - 23:38   Best Answer

Lambdas are more than inline functions, they are also closures. Any lambda that captures local variables, fields, etc, is compiled not to a method but to a type. C++ programmers are familiar with this concept under the name of functors; it was the only way to do closures before the advent of lambda expressions in C++11. C# had support for closures before lambdas actually, in C# 2.0 there were already anonymous methods which are basically lambdas with an ugly syntax. With lambdas, LINQ and the Func and Action types, C# 3.0 made functional programming much easier.



#9 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 28 June 2013 - 13:15

Oh ok, so they can function as classes as well then?



#10 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 28 June 2013 - 19:58

Oh ok, so they can function as classes as well then?

In the very limited sense that they can capture local state (fields, local variables, etc), yes. I suggest you look at the Jon Skeet article I linked to get a better understanding of closures.



#11 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 29 June 2013 - 04:28

Thank you! I will be sure and have a look. :)