[6] kotlin Lambda expression

Keywords: Lambda Java

Lambda

What is Lambda?

  • It's actually an anonymous function

Writing method

  • {[parameter list] - > [function body, last line is return value]}

Type example

  • ()->Unit 
  • No reference. Return value is Unit
  • (Int)->Int
  • Enter an integer and return an integer
  • (String,(String->String)->Boolean)
  • Incoming string, Lambda expression. Return to Boolean

call

  • Call with ()
  • Equivalent to invoke()

Simplify

  • When a function parameter is called, the last Lambda can be moved out
  • Function arguments have only one Lambda. Parentheses can be omitted when calling
  • Lambda has only one parameter that can be defaulted to it
  • Functions whose input and return values are consistent with the parameters can be passed in as arguments by function reference

 

 

 

package com.yzdzy.kotlin
//(Array<String>) -> Unit
fun main(args: Array<String>) {


    var sum = { arg1: Int, arg2: Int ->
        println("$arg1+$arg2=${arg1 + arg2}")
        //Last line return value
        arg1 + arg2
    }

    println(sum(2, 4))
    //Equal to this
    println(sum.invoke(2, 4))
    //() - > unit anonymous function is lambda
    var printLnHello = {
        println("Hello")
    }
    //Anonymous functions also have types
    //(Int) -> Long
    //Named function type () - > unit
    fun printUsage(){
        println("Please pass in two integer parameters, such as 1 2")
    }
    //println type (Any?) - > unit can be null. Back to Lambda

//    Please read the code in order it is currently 1
    println("1------------------")

    for (i in args) {
        println(i)
    }
    println("2------------------")
    args.forEach {
        println(it)
    }
    println("3------------------")
    //If the last parameter is lambda
    args.forEach({ it ->
        println(it)
    })
    println("4------------------")
// You can move the curly bracket to the front. If there is nothing in the curly bracket, you can delete it
    args.forEach() { it ->
        println(it)
    }
    println("5------------------")
    //If the function passed in is the same as the lambda parameter to be accepted
    // alt+enter can be simplified to the following by using convert lambda
    args.forEach(::println)
    //Ergodic input if x terminates the iteration and labels it to terminate the current iteration
    println("7------------------")
    args.forEach ForEach@{
        if (it == "x") return@ForEach
        //retrun is the main function. All the following 7 ------------ cannot be executed
        println(it)
    }

    println("6------------------")
    println("9------------------")
    //Function2<java.lang.Integer, java.lang.Integer, java.lang.Integer>
    println(sum)

//    You can search Function2 globally
    /*
    * /** A function that takes 2 arguments. */
public interface Function2<in P1, in P2, out R> : Function<R> {
    /** Invokes the function with the specified arguments. */
    public operator fun invoke(p1: P1, p2: P2): R
}*/

    println(printLnHello)
    println("10------------------")
    //function printUsage (Kotlin reflection is not available)
    println(::printUsage)
    //true so it's Function0
    println(::printUsage is ()->Unit)
//    Class is from Function0-22 according to the number of parameters
    //So the problem is that when the parameters are greater than 22, the functions with 23 parameters will not report errors
//    But the operation will report an error, but we usually don't pass so many parameters

    
    args.forEach {
        if (it == "x") return
        //retrun is the main function. All the following 7 ------------ cannot be executed
        println(it)
    }

    println("8------------------")

}

Posted by philippo on Sat, 09 May 2020 07:08:39 -0700