Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# lambda expressions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Lambda expressions were introduced with LINQ back
0:02 in C# 3.0, in my opinion, they are one of the most
0:06 important additions to the language along with LINQ itself.
0:09 That was groundbreaking work they did from C# 2.0
0:13 to C# 3.0, so, let's look at that really quick here
0:16 and see if Python has some kind of equivalent.
0:18 We're going to take a random list of numbers here
0:22 list of integers.
0:23 And I would like to sort them, normally you could just
0:26 do real simple stuff like data.sort, you'd be done.
0:31 However, I don't want to sort them smallest to largest
0:34 I want to sort them some other way or maybe I have a list
0:36 of customers I want to sort them by their total value
0:39 descending right so I need some function to pass to sort
0:42 to say, well, don't just sort by comparing
0:44 the items directly but apply this change
0:47 or this algorithm to each comparison
0:50 so what we can do is we pass a lambda expression here.
0:53 We say there are parameters n and m.
0:56 They go to the expression the return value.
1:01 Math.Abs(n) - Math.Abs(m)
1:04 so what we're trying to do is sort by the size
1:07 of these elements, forgetting, whether they're
1:09 negative or positive, if you could just take away all
1:11 the negative values and just do regular sort
1:13 and then put them back and that's what we're trying to do.
1:16 So we're going to print it out here.
1:17 That's a little function print collection again.
1:20 And I'd also like to, you know, maybe do some other stuff
1:23 like given those elements, I would like to convert that
1:26 to an collection that is double that
1:28 double each element in the list, after it's been sorted.
1:31 We can use little LINQ to objects here, and pass
1:34 a lambda expression to LINQ and say to the Select function
1:38 and say, given any element in the list
1:41 I want you to say the next element is two times that.
1:43 So let's just run this real quick to see what we get.
1:46 The first one is the perfect sorting 11 23
1:50 that all seems normal till you say 21, -34 55.
1:55 Notice it's sorting by amplitude
1:57 or magnitude of those numbers.
1:59 And then the second one that I pass off to the print
2:02 using that select statement actually created a new
2:05 collection that is double the first collection.
2:08 Interestingly, just like the stuff we talked about
2:11 with generators and Fibonacci sequence, this is also
2:13 one of those lazy collections
2:15 which is additionally, awesome.
2:17 These are lambda expressions, they're really great
2:20 instead of writing a whole different function of processes
2:22 I can literally write this incredibly small bit of code here
2:26 and just pass it to the select statement and modify
2:30 the behavior select or sort or things like that
2:33 really a nice feature of the language.