Getting started with Kotlin lambda Expressions
summarize
Lambda expression can be understood as an anonymous function, which is an efficient expression similar to functional programming.
In kotlin, Lambda expressions are the highest level. Lambda functions can be stored in variables, data structures, passed as arguments to, or returned from, other higher-order functions.
A higher-order function can be understood as a function whose arguments are functions or whose return values are functions.
The form of a lambda expression looks like a small piece of code wrapped into an anonymous function that is called as arguments.
The support for Lambda expressions in Koltin is very comprehensive, and using Lambda expressions well can simplify the code.
Basic use
The basic syntax structure of Lambda expressions is: {(Parameter name 1: parameter type, parameter 2: parameter type... Parameter n): Parameter type -> function body}
A standard Lambda expression looks like the argument is defined on the left side of the brace, separated by ->, and the body of the function on the right. The last line of the function body in Kotlin is the return value.
For example:
// Function type with no parameters and no return value (Unit return type cannot be omitted) {() -> Unit} // Function type with no return value {(T) -> Unit} // Function type with T type and A type with no return value (same for multiple parameters) {(T,A) -> Unit} // Function type that receives arguments of type T and returns values of type R {(T) -> R} // Function type that receives arguments of type T and type A and returns values of type R (same for multiple arguments) {(T,A) -> R}
After knowing how to look, we need to learn how to simplify, simplifying this part is very easy to lead to the situation that does not understand.
Simplification of Lambda expressions
1. You can pass a Lambda expression into a method as an argument.
val list = listOf("Apple", "Banana", "Orange", "Pear", "Watermelon") val lambda = { (fruit: String) -> fruit.length} // lambda expression as variable // Note the parentheses here, Lambda argument received in parentheses val maxLengthFruit = list.maxBy(lambda) // Passed val maxLengthFruit = list.maxBy({fruit: String -> fruit.length}) // Simplify 1 println(" fruit with the longest word: "+ maxLengthFruit)
2, when the variable is the last variable and it is a lambda expression, you can move the expression after the parentheses:
val list = listOf("Apple", "Banana", "Orange", "Pear", "Watermelon") //val lambda = { fruit: String -> fruit.length} // lambda expression as variable // Note the parentheses here, Lambda argument received in parentheses // val maxLengthFruit = list.maxBy(lambda) // Passed in // val maxLengthFruit = list.maxBy({fruit: String -> fruit.length}) // Simplify 1 val maxLengthFruit = list.maxBy(){fruit: String -> fruit.length} // Simplify 2 println(" The longest word fruit: "+ maxLengthFruit)
3, when the variable is the only variable, you can omit the parentheses:
val list = listOf("Apple", "Banana", "Orange", "Pear", "Watermelon") //val lambda = { fruit: String -> fruit.length} // lambda expression as variable // Note the parentheses here, Lambda argument received in parentheses // val maxLengthFruit = list.maxBy(lambda) // Passed in // val maxLengthFruit = list.maxBy({fruit: String -> fruit.length}) // Simplify 1 // val maxLengthFruit = list.maxBy(){fruit: String -> Fruit. length} // Simplify 2 val maxLengthFruit = list.maxBy{fruit: String -> Fruit. length} // Simplify 3 println(" The fruit with the longest word: " + maxLengthFruit)
4. Use kotlin's type inference and omit type declarations:
val list = listOf("Apple", "Banana", "Orange", "Pear", "Watermelon") //val lambda = { fruit: String -> fruit.length} // lambda expression as variable // Note the parentheses here, Lambda argument received in parentheses // val maxLengthFruit = list.maxBy(lambda) // Passed in // val maxLengthFruit = list.maxBy({fruit: String -> fruit.length}) // Simplify 1 // val maxLengthFruit = list.maxBy(){fruit: String -> fruit.length} // Simplify 2 // val maxLengthFruit = list.maxBy{fruit: String -> Fruit. length} // Simplified 3 val maxLengthFruit = list.maxBy{fruit -> Fruit. length} // Simplified 4 println(" The fruit with the longest word: " + maxLengthFruit)
5, when the variable has only one parameter, you can use it to refer to:
val list = listOf("Apple", "Banana", "Orange", "Pear", "Watermelon") //val lambda = { fruit: String -> fruit.length} // lambda expression as variable // Note the parentheses here, Lambda argument received in parentheses // val maxLengthFruit = list.maxBy(lambda) // Passed in // val maxLengthFruit = list.maxBy({fruit: String -> fruit.length}) // Simplify 1 // val maxLengthFruit = list.maxBy(){fruit: String -> fruit.length} // Simplify 2 // val maxLengthFruit = list.maxBy{fruit: String -> Fruit. length} // Simplify 3 // val maxLengthFruit = list.maxBy{fruit -> Fruit. length} // Simplify 4 val maxLengthFruit = list.maxBy{it-length} // Simplified 5 println(" fruit with the longest word: "+ maxLengthFruit)
You can understand these, basically the basic use and reading of Lambda expressions in koltin will be no problem!
This article on the Kotlin lambda expression introduction guide to this article, more related to Kotlin lambda expression content please search Script House previous articles or continue to browse the following related articles hope that you will support Script House in the future!
- The application and parsing examples of List Lambda expression in Kotlin are detailed
- Kotlin uses Lambda to set the operation of the callback
- Kotlin basic learning lambda return statement explanation
- Play with Kotlin to get to grips with Lambda and higher-order functions
- Detail Kotlin higher-order functions and Lambda expressions
Related article
-
Android Internet access to the image and display in the client method
This article mainly introduces the method of Android Internet access pictures and display in the client, combined with examples to analyze the skills of Android processing pictures, and with Android URL encapsulation class, network connection encapsulation class and output stream encapsulation class, the need of friends can refer to the next2015-12-12 -
Example of TextView method for Android to implement the number jumping effect
Digital jumping effect I believe you should have seen, in the development of this effect will make the ui interaction look very good, so the following article mainly introduces the Android to achieve the TextView of the relevant information, the article gives a detailed example code, the need for friends can refer to learn, let's take a look.2017-04-04 -
How to bind an Android item to an MVVM project template
This article mainly introduces the relevant information about the Android single binding MVVM project template, the article introduces very detailed through the example code, for you Android developers have a certain reference learning value, need friends to learn it together2019-04-04 -
android Asynchronous Task Design Ideas (AsyncTask)
AsyncTask is very common in Android, so why is it so common, do not do it, how is it implemented internally, why does Java API not have this class, after reading this article, you will know the answer2014-02-02 -
Simple use of DrawerLayout for Android drawer Layout
This article mainly introduces the simple use of DrawerLayout for Android drawer layout in detail. The example code in this article is very detailed and has certain reference value. Interested partners can refer to it2021-06-06 -
Android uses Handler to download files
This article mainly introduces in detail the function of Android using Handler to download files, which has certain reference value, interested friends can refer to it2018-06-06 -
Android uses the orientation sensor to obtain the relative Angle of the phone example
The following is an example to introduce you to the use of Android to obtain the relative Angle of the phone by the direction sensor, do not know friends can refer to2013-06-06 -
ijkplayer package supports https so use details
This article mainly introduces the detailed explanation of the use of so that ijkplayer packaging supports https. Friends in need can use it for reference. I hope it can be helpful2022-11-11 -
Android enables the screen to keep steady on
This article mainly introduces the Android screen to keep the function in detail, the example code in the article is very detailed, has a certain reference value, interested partners can refer to it2022-01-01
Latest comments