Kotlin Newbie to Pro - LAMBDA FUNCTIONS - Part 28

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys welcome back to the 28th part of the cotton newbie Pro Series in the last part I told you what exceptions are and how to deal with them so we created a function divided here that divides two double numbers and if the second number is equal to zero then we throw a custom division by zero exception and when we want to use that function we had to use it inside of a try-catch block so when that exception occurs when we divide by zero here then it will jump into that catch block and catch that division by zero exception and assign the value 0 instead to our division variable this video will be about lambda functions which are used to pass functions as parameters to other functions this can be a little bit difficult if you never used them but I will try my best to explain that in very detail to start simple I want to show you some lambda functions you can apply on lists so let's create a list here of our list is equal to I I want to create a list from 1 to 20 here so I'll create a range want to trendy and convert that to a list so dot truelist after that so that list will just contain all the numbers form from 1 to 20 and afterwards I just want to print that list and after we print it then we can apply for example a filter function on that list so the filter function is a lambda function so if we write list dot filter you can see that after that function there are two curly brackets and you can recognize lambda functions in the function recommendations here when there are two curly brackets after that so press ENTER and now you can see that we don't call that function like we call a normal function with parentheses after that instead we write you curly brackets after that and to actually know what we have to put in those curly brackets we can click into those curly records and press ctrl + P and then you see what kind of function the filter function wants from us here and it runs a function that takes one integer as an argument and returns a boolean so the integer in this case is each item of that list once so the filter function will basically go through that whole list and each item will one time be that integer we have here and the boolean is whether we want to keep that item in the list or not so the function is used to filter some items out and let's say we want to filter out all the odd numbers in that list then we can write it in this case it refers to the current number so that filter function will go through all the numbers from 1 to 20 and each number will one time be it which probably stands for items so the current item then we check if it modulo 2 is equal to zero so if it is an even number so we check for each number if it is even and if it is then we want to keep that in the list so then that expression would be true and if that expression is false then we want to filter out that item so let's print the list afterwards and if we now run that program you see first it prints that hole is from 1 to 20 and afterwards it filtered out all the odd numbers so we checked for each number if it is divisible by two and if it is that means we want to keep it in the list if it is not divisible by two that means we want to remove it from the list and that is actually what happened here so we only have the even numbers now in the list and that's the cool thing about lambda functions we can call basically a function that already exists the filter function but we can provide our own implementation of how we want to filter so we could also write it is greater than eight for example and then it will filter out all the numbers that are less than eight less or equal than eight so let's run that and you can see now we only get those numbers in the list that are greater than eight so that means we can just put any implementation any boolean here in it and the filter function will check for us if that item should be in the list or should not be in the list and to show you that you can apply that those lambda functions to all lists I created some shapes here so true circles to triangles and to rectangles just inserted some values and then I created the list and put all those shapes I created in it and now I want to show you that we can also use that filter function to filter out shapes so let's write shapes is equal to shapes top filter and now in that case you can see that this is not an integer anymore instead it's a shape so if we now write it then we actually refer to a shape and not you an integer anymore so let's say we want to keep all the shapes in the list that have an area greater than 20 so it dot area because we know that it is a shape we can call the area function on it and if that area is greater than 20 we want to keep that shape in the list so let's write a short for loop here to print out the list because we don't have a function that just prints a shape so I'll write for shape in shapes and print the shapes name shape dot name with the colon and print that the area of that shape is equal to shaped area so when we now run the program you can see that it of course prints a lot of stuff here because we created some shapes and we still have those print line statements in the shape classes but if we scroll down you see that it prints three shapes here two circles and rectangle that have earned an area that is greater than 20 so now we filter out all the shapes that have an area less or equal than 20 and to show you another lambda function for lists we can write that after that list after that filter function so write a dot after that and you can actually look in that function list for longer functions for example here's the lambda function that filter function we used before sorted by is a function distinct by and all those functions have a different purpose so we can just try it try around with them a little bit but for now I want to show you the sorted by function press ENTER here and inside of that sorted by function we now tell Kotlin that we want to sort our shaped list and we have to provide a value by which it is sorted so let's say we want to sort that list by the size of the area then we can say ID dot area so now take a look at those three shapes here and actually the areas of those three shapes when we run the program again you see that now those three shapes are sorted by the area so they are sorted ascending by the areas and that's really cool with the sorted by function that we can define our own rules to sort our own lists what I want to show you now is actually a little bit more complicated because I want to create our very own filter function so I want to show you how we can actually create our own lambda functions so a function we can pass another function as a parameter to do that I will jump right below here and write function and I want to make a function that just has the same functionality as the filter function but I just want to make it for our for a list of shapes to keep it simple so function list of shape we need to make that as an extension function because we want to call that function that filter function we will create now on a list of shapes so this is why we have to make it an extension function so we write list of shape dot and then I'll call it custom filter so inside of the parameters here we have to tell Kotlin what kind of function we want to have this function passed to so I'll call it filter function first write a colon after that and now we have to specify the type of that function and we want this function to take a shape as a parameter because that it here is a shape too and the return type of that function so we have to make an arrow like that is a boolean because we want to check if our certain condition is true for that shape and if it is we want to keep that item in the list in that list here we call the function on and that whole function returns a list of shape again so that is why we can assign that filter function to our shaped list again and curly brackets after that so I very much hope that this syntax got clear to you so what we do here is we define an extension function that we can call on a list of shapes that extension function is called custom filter and it takes as a parameter a filter function and that filter function we pass as a parameter takes as a parameter a shape and returns a boolean and our custom filter function returns a list of shapes so the list we apply the filter on this filter function here so inside of that function I want to create a result list which is a mutable list of shapes at first it is empty and now I want to loop through this list here and check for each item if this filter function evaluates to true and if it evaluates to true we want to add that item to our result list so we loop through our list for each shape in this and if you remember this refers to our list we call that custom filter function on curly brackets after that and now we want to check for each shape so we loop through that list and check for each shape in that list if our filter function with the shape passed as a parameter so that shape will later be it and if that filter function values are true because the result of that function is a boolean then we can just call that function here and it will evaluate to true or false so we can just check it inside of the if statement curly brackets after that and in that case we found the shape that that we want to keep in that list so we add that item to our result list which also store add shape and finally after that for loop we just return our result list so let's replace that filter function here with our custom filter function and run the program and you can see it gives us the exact same output so our custom filter function does the exact same thing as the filter function does that is already in the Kotlin standard library one more important thing I have to show you is when you have several parameters in your function that you pass as a parameter so let's say that filter function also takes the string for example which wouldn't make much sense here but just to show you then we of course have to pass a string to our filter function here too so just use hello for example just for demonstration and you can see now we can't refer to the current item with it anymore because it doesn't know if it is now the shape or the string so in that case let's write that again custom filter if you press ENTER here on that function then you can see that we can now name our two parameters here so we can refer to the shape with shape here and to the string with s or we can give any other name for example just string then we have to make that little arrow again and press ENTER and now we can just filter like we did before and we can access that shape here with shape and that string here with string so we can just write shape dot area is greater than 20 so we don't need to write it that area here in step 3 right shape dot area because we named the parameter shape shape so your main homework for this video is to just understand what happened here but if you want to have a little challenge then you can also create a function custom sum and that function should be applied to lists of integers and also take a boolean condition here and then it should only sum up those numbers for which that condition is true so in that case we want to only sum up those numbers that are odd so 1 plus 3 plus 5 plus 7 plus 9 in this example because the list goes from 1 to 10 and this evaluates to 25 and if we run this you see it prints the sum is 25 the homework from the last video was about you throw an exception when we create a circle with a negative radius so that was actually pretty simple to do we just check inside of the unit block if our radius is less than zero in that case we throw a negative radius exception which I created above here you can also create that class inside a separate file or inside of another file but for simplicity I just created that class inside of the circle class because those two classes are related together so that's it for this video I hope this got clear because that's really complicated topic with lambda functions and you have to really try around with that a little bit if you have any questions then don't mind to ask me in the comments then I will answer them and yeah have a good day see you in the next video bye bye
Info
Channel: Philipp Lackner
Views: 9,734
Rating: 4.9411764 out of 5
Keywords:
Id: wnyN8umZIRM
Channel Id: undefined
Length: 16min 29sec (989 seconds)
Published: Mon Jan 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.