#9.1 Kotlin Lambdas and Higher Order Functions Part - 1. Kotlin Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi registering Siddarth and welcome to the next video of the scotland programming series now in this video we will talk about high-level functions and lambdas expressions now as a beginner you might feel the difficulty to understand these two concepts but I will try to explain you each and every concept in detail so I will simply divided this module in the sub sections so in this video we will have the brief introduction of the high-level functions and lambdas with the help of a small demo in which we will try to implement a code with the help of object-oriented programming way using the interface and then we will use how to use the high-level functions and then we will use the lambdas expressions and in the next video we will talk about the lambdas and high-level functions in deep detail with the help of explanation as well as demo so this video is all about the introduction video so what are high-level functions in Kotlin now in Cortlandt we have functions that accepts functions as a parameter so instead of passing a string or integer we can simply pass a function as a parameter to some other function right and then from a function we can also return a particular function and also at last we can have functions that can either accept the functions as a parameter and return of function as a value so if a function can satisfy any of these three criteria then that function is known as the high-level functions now next comes the lambdas now the lambdas in a simple words can be defined as it is just a function without any name now trust me the concept of high-level function and the lambdas is very very important if you want to jump into the Android development with Cortland as a programming language so make sure you understand all these concepts of high-level functions and lambdas in this 4/5 videos that I am going to show you in this module so make sure you watch each and every second of this module so inside the IntelliJ IDE having a class program inside which I am having a simple method to add two numbers that takes a and B as a parameter find the sum and then print the sum right and inside the main function I am simply creating the object of the program and then calling this function so if I run this code right now we get nine in the output console no doubt in that now now the same program can be written with the help of an interface that is when we are going to print the sum of these two numbers we are simply going to use the interface so for that let us define the interface down the side so here I have simply defined my interface with a function of execute that takes a parameter of integer value let's say sum if you don't know what is the interface then you can simply call this interface as a listener suppose on click of a button something should happen so what should happen will be determined with the help of this interface right like in Android we have button dot on click listener on touch listener on double-click listener and so on all those are actually interface in android or java right so similarly I have simply defined the interface having this method of execute so let us now proceed forward and let us copy this method and paste it at the top inside the class program let me change the commit line so I am simply going to use the same method and print the sum with the help of interface right so here as a third parameter I will simply call I will simply define the third parameter as let's say action and then the name of the interface as my interface now this my interface is user defined so you can change this name as per your wish and this method is also user-defined right and now inside this what I will do is suppose this println is actually the body let us assume it right so here as well this println is actually the body so in place of println what I will do is I will simply use action dot let's say execute and simply pass some as a parameter and now let us leave this statement as of now here and now inside the main method let us try to call this add two numbers method that has a third parameter as the interface so what I will do is I will simply called program dot add two numbers and I will simply select the second one that takes three parameters to let's say seven and the third parameter will be the interface now if you are from a Java background then you must be knowing that you cannot instantiate or create the object of an interface you can never do that so how can we use this interface so for that as per the Java we need to first create a class that implements this interface right and right after that we can simply implement this method that will be called when this statement will be executed so in case of Java or in Android we used to have new on click listener something like that and inside this we actually use to implement the on click method right but in case of Cortland since we don't have a new keyword that creates the new instance that implements this interface so in place of this we have the object keyword so define the object keyword followed by colon then followed by my interface now here I will simply define the body of this my interface and then hit enter now here it shows some error that we need to implement the method so simply press alt' + enter' and implement the member and then head on okay so here the IntelliJ IDE has simply created the execute method for us so here we have the override notation so inside this we have to define the method body now our body in our case is print Ln sum so simply copy it or cut it from here and paste it inside this overridden execute method now if I run the code right now let's see what happens so here in the output console we get 9 again so this shows that by using the object-oriented way that is by using the interface we have simply got the same output right now let us come to the point now even if you have not understood what I have done here using the interface then please do not worry about it because the main motive of this video is to understand the lambdas and high-level functions now at the end let us come to the point let us use the lambdas and high level functions and try to get the sum of these two numbers using the lambdas right now in order to use a lambda first we have to define a function that is the high level function that can accept a function within a function so let us define a new function and then let us change the command line and now here as a third parameter I won't use this interface I will simply use some lambda expression that I will talk about it shortly and then we will change the body as well here now here inside the main method what I will do is I will simply define a lambda expression now how to define a lambda is that use the curly braces open and close and then you have to define a variable let's say let's call it s now followed by - followed by arrow and then the body the body is actually print line sum so simply copy it and paste it here and instead of sum let's call it s now whatever we have written inside these two curly bracket are actually the lambda function or expression so the lambda expression is nothing but just a function now why do we call it as a function now just because this is actually the function without any name now suppose here I have add two numbers this is a function with a name add two numbers but here this lambda expression which is actually the function has no name it has just some code written inside it now here this s is actually a parameter like int a and int B here are the parameters inside the add two numbers similarly here this s is actually acting as a parameter then right after this operator of arrow you will find print Ln s now this is actually the body of the method right similar to what we have written here this is the body of the method of active numbers similarly print Ln s is actually the body of the lambda expression or function now here it shows some error that I will talk about it shortly so but before that here let us define let's say Val test of the type of strings equal to let's say hello so here I simply defined a variable of test of the type of string equal to some value of hello right now similar to this this lambda expression can also be assigned to some variable such as test so here what I will do is I will simply define the Val let's say my lambda equal to s followed by the method body this is actually the parameter and then we have the method body don't forget it now the compiler wants to know the type of this s variable so this s is actually of the type of end now here the error will be gone now instead of declaring the SS int we can also define the type of expression to this variable of my lambda similar to what we have done here this is a string hello so we have simply declared that this hello is actually of the type of string so this test will now become string right so similarly my lambda followed by colon and then what I will do is the first one is actually the parameter which is of the type of int so here bracket open and close inside this let us use I and T which is actually the parameter then followed by - arrow and then this print Ln s now what is the type of the body which means that what exactly you are returning from this method now since I told you print LNS is actually the content of the body of this method so this println method does not return anything it returns void or unit so here what I will do is I will simply write unit right now suppose as a body if we had let us say 2 plus 3 which actually returns the integer value 5 so here I will simply write int fine similarly if we have here let's say hello then this body will actually return the string so here instead of int I will simply write string right now here since let us get get back to originality we had the print LNS that simply returns nothing which means unit right so this completes our statement of lambda now still if you have any doubt then please check out my next video where I will talk about what are the content of a lambda expression but as of now let us proceed ahead and now what I will do is I will simply try to call this function now remember this my lambda is actually a function so here if I call let's say program dot let's say add two numbers and then simply pass 2 comma 7 now the third parameter I want to pass this my lambda so let us call it my lambda since this my lambda is actually the function that we are passing to another function right so that is why this function is now acting as a high level function with that simply accepts lambda as a parameter that is a function as a parameter so here my lambda will fall here so here I have to define the name of the variable for this function let us call it the same one as action so let's here called action followed by colon then give it a space and now here since a and B are of the type of integer so this action function should also can't some type and that type is actually coming from here integer followed by unit right command C and command V so when we pass by lambda which is actually the function coming from end going into unit so we can read it as this function action coming from a end to a unit so this unit is actually the return type of this print Ln s whatever value we are returning from the body right here if you notice inside this method of actin numbers we have the body of print Ln some now this body we have already defined inside the print Ln s that is inside the lambda expression so what we can do here is we can simply comment this statement and instead of this statement I will simply call action as a parameter simply pass some now when the code will be compiled then this statement action parameters sum will actually be replaced by print line s right so what we are doing here is this sum will now be substituted by this s and this S which is going into the body of print line s so here we are simply getting the sum print line sum or just to avoid confusion let us call print line sum so when the code will be compiled the action sum will actually be converted to this print line sum and in the output we will get the sum of a and B that is 9 so let us not run the code and let's see what happens so in the output 10 so will we again get 9 so this shows that our code is now working perfectly fine again with the help of high level function with lambda as a parameter now we can simply remove this code from here so here in front of you I have simply shown you a very simple way and then using the interface or the object-oriented way and then finally the comparison of these two methods with the high level functions with lambda as parameter now at the end I will simply conclude video by simply telling you dad we can directly put this lambda expression control X or control C in place of this my lambda variable so when this statement will be executed we are simply passing the lambda expression directly but earlier we were using the my lambda variable so the code will execute fine now again at last you must be thinking if we were able to achieve the same thing with the help of this simple method and by using the interface as well then why do we need to have this lambda expression and high-level functions but trust me then you will enter into the Android application development you will be surrounded by lambda expressions and high level functions all around so for that you have to simply clear your concept about these two concepts right here only and in case you have not understood the lambda expression and high level function in this video then please check out my next video where I will talk about these two concepts in deep detail this is shrinkage are signing off and thanks for watching have a good day thank you
Info
Channel: Smartherd
Views: 63,285
Rating: 4.7860227 out of 5
Keywords: android, smartherd, kotlin, lambdas, lambda expression, kotlin for android, kotlin tutorial, high level functions, functions, methods, functional programming, higher order functions, higher-order functions
Id: S0Vkldrh0SE
Channel Id: undefined
Length: 16min 47sec (1007 seconds)
Published: Fri Jun 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.