Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.1 Lambdas and Higher-Order Functions

Hello all. In the previous videos, we had seen the basic syntax of Kotlin. How do we use the loops and iterators? How do we use the control statements? And we learned many more basic concepts of Kotlin. In this lesson, we will have a sneak peak at the advanced features of Kotlin. So, let's get started with Lambda and High Level Functions. So what basically Lambda is? Lambda is the function without the name. I have obtained a block of code here, with the main function in the Kotlin. And then I have a class Demo with the function here. This function has the name getGreater, but Lambdas are the functions without the name. Next comes the High Level Functions. The High Level Functions are the ones which accept functions as the argument. In this function, we have two arguments, but they are off in detail type. If this function had a function as an argument, it would have been a High Level Function. The High Level Functions also return the function. So, if a function accepts a function, or returns a function, or accept and return a function, it may be known as the High Level Function. The High Level Functions and Lambdas are important for Android Application Development. Then we move into Android using Kotlin. It will mainly be used in case of call back functions. So that's the latter part. We will have a look at how Lambdas and High Level Functions work. Here I have a basic example. The kind of rates we have already logged on. Let this coding ensure that everything is working fine. So we have the result here which says Greater is 7. Of course, 7 is greater than 4. So we get the result. Now let us see how do we use High Level Functions and Lambdas in Kotlin. So how does a Lambda look? Like we have the body of the function inside code replaces, same way we will have the body of the Lambda inside code replaces. In the body of the Lambda, we will have num which is of type Int. And the body of the Lambda will be, let's just keep it simple and print num is $num. Now, here is our Lambda. This is the parameter that the Lambda function will use which is of integer type, like we have here num1 is Int, in the Lambda the argument is of Int type. And then here is the function body for the Lambda which uses this argument. So, we have the Lambda defined inside the curly braces. But we don't have a name here like the general function. We can also assign this Lambda to a variable. Let us assign this to a variable, say, testLambda. Now how do we pass this Lambda as a parameter to some other function? Let us copy this function here and have the third parameter as Lambda. Let's name it as myLambda and before that, we need to define one more thing. The error for the Lambda parameter says, a type annotation is required on a value parameter. So for this testLambda, we need to define the type like we defined for other variables. For this num, we have the type Int. Same way for Lambda, we need to define the type. So, how do we define the type for Lambda? Based on the body of the Lambda, we see the Lambda function takes the integer value and returns nothing. All we can see it returns void. And in Kotlin we can see it returns unit. So how can we define the annotation for the Lambda? The syntax is :, type here Int. Int comes from the type of argument the Lambda will have. Though we can type that the Lambda were written, here it is unit. So, we read it as testlambda int to unit. Which means it accepts the integer type and returns the unit type. Same way we will define this here with the Lambda in the parameter. So we have a function here, function getGreater, first parameter Int, second parameter Int, and the third parameter is the Lambda function. Now we need to call this function from our main function. How do we do that? Here we have seen how we defined the testLambda. Now to use this testLambda as parameter to call a function, we will use the object of the Demo class. And then we need the function of int to unit type, which is the testLambda here. So we pass testLambda. Let's comment this out for a moment. So we have testLambda here. The testlambda in its body has println("num is $num"). So, we will bring the Greater number here. And the Lambda has the body as println("num is $num"). So here, first we find out the bigger number and assign it to big. After doing this, instead of printing this using the println, let us see how we can use the Lambda to execute this. We will comment this out and now we will use myLambda. MyLambda accepts the Int type. So, myLambda, giving it the Int type which is big here, we will get the statement printed here, num is $num. Let us run this code for now and see what happens. Here we have the num is 8. So, how is it read? So here, this big is passed to the num which is of Int type and the unit type is returned. So instead of writing num is $num, let us write Greater is num. Let us change it to this and we have the result as Greater is 8. So this is how we use Lambdas in our Kotlin. And here, getGreater becomes the High Level Function since it accepts Lambda as one of its parameter. Summarizing all of it, Lambda is the function without the function name. It accepts the parameter and it has the function body. The annotation or the type of the Lambda has to be defined while we're assigning it to a variable. And how do we do that? The parameter which the Lambda accepts is this and the type of data that Lambda returns is this. So here, we have in two unit type of Lambda. And then, we pass this Lambda as normal arguments while calling a function. So we are clear with the Lambda here. Coming to the High Level Function which accepts the function as its parameter. We have a normal function here, which accepts two integer arguments and the third one is a function. How can we use this function in a function body? We found out a result and stored it in the variable and now we are passing this variable to our Lambda function. This big is the num here and we have used this num to execute this function body. Last but not the least, instead of passing this Lambda as a variable here, we can pass it as it is in formal function. So instead of testlambda, we can have the Lambda passed this way. So now let us see what happens. Let us pass 0 here and see how that would comes. We get the Greater is 4 and hence we get the output from the updated code. That was all for the Lambdas and the High Level Functions. In the upcoming lesson, we will learn about how do we call a Java function from Kotlin file and Kotlin function from Java file. Keep exploring and keep coding.

Back to the top