Play with kotlin and thoroughly understand lambda and higher-order functions

What is lambda

In short, lambda is a representation of a function (implying that a lambda expression is equal to a function). More specifically, lambda is an undeclared function that is passed as an expression

Why lambda

Imagine implementing a view click event in Android, which can be implemented as follows:

If lambda is used in kotlin, the implementation can be as follows:

It is obvious that on the one hand, lambda can save a lot of code. The most important thing is that lambda expressions can avoid writing explicit function declarations in abstract classes or interfaces, and then avoid the implementation part of classes (the onclicklistener interface is omitted)

Lambda expression syntax:

1. Lambda expressions are always surrounded by braces; 2. Its parameters (if any) are declared before - > parameter type can be omitted; 3. The function body (if any) is after - > function

The specific writing methods can be as follows:

Analyze the above two expressions:

The first one is easy to understand. First, a variable sum1 is declared on the left of '=' and a labmda expression is assigned to sum1 on the right of '='. The second one is a little more complex, mainly sum2 on the left: what does the following lump mean. First of all, children's shoes familiar with the kotlin language should know that the kotlin function parameters are defined using Pascal notation (Name: type), so sum2: the following lump represents a type. What type does it specifically represent? In kotlin, everything is an object, including a function. Since it is an object, like integer, string and other objects, a function also has its own type type type (X: int, j: int) - > int. this expression is to express the type of the function. It represents a function that needs to pass in two int type parameters and return int type. So how do you express a function that has no parameters and returns a string type? See the first floor for the answer

Lambda pass through usage

When we need to use these two lambda expressions, we can directly pass sum1 and sum2 to a higher-order function (explained later), or we can directly pass the expression after = to a higher-order function, as shown below:

Next, let's take a look at how the above view. Setonclicklistener {V - > imageclicked (V)} evolved step by step. Before that, we need to know what higher-order functions are

What is the higher order function

Functions that take functions as arguments or return functions are called higher-order functions

Define a higher-order function

After knowing what a higher-order function is, we can use a piece of pseudo code to demonstrate how to define a higher-order function, as follows:

Higher order function name (parameter function name: parameter function type): return type of higher order function {higher order function body...}

Note: let's name the function passed in as a parameter as a parameter function

Write a specific implementation as follows:

In the above specific example, we defined a high-order function named highorderfunc and passed in three parameters. The first two parameters are int type, the last parameter is a function, and the function type is to pass in two int parameters and return Boolean type values. Finally, the return type of this higher-order function is int value

Using higher order functions

After defining a higher-order function, we can pass a lambda to the higher-order function. The complete example is as follows:

You can see that except sum1 and sum2, a lambda function Val max = {X: int, Y: int - > x > y} is redefined, and this lambda is passed to the previously defined high-order function highorderfunc. The meaning expressed by this synthesis is to find the larger of the two parameters passed in.

The final printing results are as follows:

Function2<java.lang.Integer,java.lang.Integer,java.lang.Integer> Function2<java.lang.Integer,java.lang.Integer> 30 biggerNum is 80

Note: the printed results of println (sum1) and println (sum2) are both function2, which is an object of kotlin and represents a function type

analysis

After understanding the definition and use of higher-order functions, let's go back and understand how the expression view. Setonclicklistener {V - > imageclicked (V)} evolved step by step. First, we can write a complete lambda as follows:

Declare a function variable imageclick and point to a lambda function {V - > viewclicked (V)}. In the Lambda function body, the viewClicked (v: View?) method is called. Then you can call this method. The complete code is as follows:

Lambda expressions can also be passed to a higher-order function as parameters, so in the above code, view. Setonclicklistener (imageclick), = > view. Setonclicklistener ({V - > viewclicked (V)})

There is a convention in kotlin. If the last parameter of a function is a function and you pass a lambda expression as the corresponding parameter, you can specify it outside the parentheses. Therefore, you can implement the following view. Setonclicklistener ({V - > viewclicked (V)}) = > view. Setonclicklistener() {V - > viewclicked (V)}

There is another convention in kotlin. If a function has only one parameter and the parameter is also a function, the parentheses view. Setonclicklistener() {V - > viewclicked (V)} = > view. Setonclicklistener {V - > viewclicked (V)}

Summary:

Lambda and higher-order functions are a bit confusing to understand, and it takes a lot of practice and experiments to understand them slowly (some complex codes are written more and used to, so naturally there is no reason to write like this, ha ha)

At the beginning of the article, we said that using lambda can omit the link of interface definition and implementation, but it is conditional. Only one abstract method of this interface needs to be implemented can lambda be used instead (such as onclicklistener and onitemclicklistener). If there are more than one abstract method, lambda cannot be used for substitution (such as onitemselectedlistener). See the following code for details:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>