Getting Started with Kotlin lambda Expressions _Android_ Home of Scripts

Getting started with Kotlin lambda Expressions

Updated: Mar 01, 2024 10:19:08 by Nimrod__
In kotlin,Lambda expression is the highest level,Lambda expression can be understood as an anonymous function, is an efficient expression similar to functional programming, this article introduces Kotlin lambda expression introduction guide, interested friends take a look at it

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!

Related article

Latest comments