Lambda expressions can reduce your code, make it more readable and turn you into a more efficient programmer. I'll give you the rundown on what lambda is, how it's built and show you a bunch of useful lambda functions. Lastly I'll show you how to create your own lambda functions.
Give the video a like if you enjoyed it! Also, come join the discord: https://discord.gg/GqeHHnhHpz
if you boys are not using lambda yet well you need to focus on me because i'm just going to introduce you to a wonderful world so come on let's go say we have a list of integers and we'd like to create a new list of all the numbers which are over five traditionally you would do something like this but with lambdas you can do the same thing but with much less code let's break it down we begin by referencing the list we'd like to filter then we call a function which is suitable to the situation in this case i'd like to grab a subset from this list so i'll use the where function we're saying give me the items from this list where my condition is met in the parentheses we start by supplying the argument to the function this can be looked at the same as an argument given to a normal function if it's more your style you can give your lambda variables more descriptive names but this is entirely up to you i much prefer short concise names following is the arrow function or more suitably known here as the lambda operator this allows you to create an anonymous function which is just an inline function without a name the variable and lambda operator is the equivalent to this section of a real function last comes the actual function logic the where clause expects a boolean value to determine if this item in the list should be included in the new list this section is equivalent to the inner logic of a normal function so to summarize the where clause will run this section of code for each item in the list and return the current item if it's greater than five let's explore some additional lambda methods here we have a list of steam games let's say we'd like to know if all the games have a rating of at least 9 and above for this we can use the all function as you can see it follows the same syntactical conventions the where function does here we're asking if all the items satisfy our condition to which the answer is no cyberpunk destroyed our condition just like it destroyed our hopes and dreams without lambda this code might look something like this okay what if you'd like to grab a list of the names we can use the select function to do just that by the way innumerable is a collection interface it allows us to establish the idea for a list without actually processing it only when you call on it does it get computed lazy loading if you will we can force it to process immediately by using the to list function select is very powerful as it allows you to create entirely new objects let's say we have another object called star rating which has the name of the game and a star rating out of five we can take our game list and return a new star rating object using the properties of the original item we'll divide our rating by 2 as the max star rating is 5 not 10. another common function is first this will find the first item which satisfies the condition and return it but be careful this will throw an invalid operation exception if the list contains no elements which match an alternative strategy is to use first or default this will return an item if it finds one otherwise the variable will be null okay let's end with something interesting let's say we work for steam and we need to recommend three games to the user on the home screen the games need to fulfill the following criteria they need to have at least a score of nine they need to be released after the year 2018 and the games must be randomly selected so that the user gets a different set of games each time he visits the homepage i've added some additional games to make it a bit more interesting first let's assemble a list of games which satisfies our demands just as a traditional if statement we can use the and operator to check for multiple conditions the beautiful thing about lambda expressions is that we can string together multiple functions with ease so now that we have our filtered games let's shuffle them to ensure we're presenting a random set to the user we can use the order by function to do just this whatever value we return from this function is what will be used to order the items for example we could place the game score here or the release date but we want to randomize this list so let's use the random class to just return a random value now the list is shuffled we need to grab three values from the list we can use the take function to do this take will grab the first three items from the list which is fine for us as it's all been shuffled around pretty freaking snazzy right one last thing i'd like to show you is how to create your own lambda function i urge you to take a look into the many wonderful functions available to you what i've shown you today is just a tiny glimpse of its offerings