preface

This article mainly introduces kotlin functions, higher-order functions, simple use of lambda expressions

function

A function is a fragment of program execution, defined in Kotlin as follows:

fun function(x:Int,y:Int):Int{
        return  x+y;
    }
Copy the code

Where () is the input parameter, and the returned type after:. When the returned value is Unit(which can be understood as the Java keyword void), the returned value type can be ignored, that is

fun function(x:Int,y:Int){
          x+y;
    }
Copy the code

This is the way a function is defined as a code block function body, or an expression function body, as shown below.

fun function(x:Int,y:Int):Int=x+y;
Copy the code

The test results are as follows:

10-31 14:18:50.106 29182-29182/com.zhqy.javademo E/result: 3
Copy the code

The body of an expression function is a function definition with {} removed, and the type of return is not indicated here. This is because Kotlin supports type derivation, which deduces the type of return reference from the type of the parameter, but this derivation is limited to the underlying data type. So when do you need to annotate data types, and when don’t? This summary is as follows: (1) if it is a function argument — the parameter type must be declared. (2) The body of a non-expression function — all but the Unit type is required. (3) the return value of a public method — the type is recommended

Higher-order functions

The so-called high-order function is that the function can be used as parameters of other functions. This kind of function that contains parameters of function type is called high-order function. Higher-order functions have the following form:

 fun<T> gjfun(t:T,condition:(t:T)->Boolean):Boolean{
        return condition(t);
    }
Copy the code

Since functions can be passed as arguments to another function, we have to mention function types, which Kotlin declares as follows:

(Variable name: data type)-> Return value type (t: t)->BooleanCopy the code

The test code is as follows:

   fun<T> isOdd(t:T,condition:(t:T)->Boolean):Boolean{
        return  condition(t);
    }


 val odd = isOdd(10, { x: Int -> x % 2 == 0 })
        Log.e("result","${odd}");
Copy the code

Test results:

10-31 14:42:54.384 30147-30147/com.zhqy.javademo E/result: true
Copy the code

{x: Int -> x % 2 == 0} So this is the Lambda expression. To create a function in the form of lambda syntax and pass it into a function, which is a bit of an anonymous function, you can also do this in the following ways:

Var noNameFunction=fun(x:Int):Boolean{return x%2==0; var noNameFunction=fun(x:Int):Boolean{return x%2==0; } val odd = isOdd(10, noNameFunction) Log.e("result","$odd")Copy the code
Val odd = isOdd(10){x:Int->x%2==0}Copy the code

From this you can see that there are many equivalents of higher-order functions, which I think is one of the reasons why Kotlin’s learning cost is so high.

Define a lambda expression as follows:

{parameters -> function body}Copy the code

The following is an example:

   val function:(Int,Int)->Int={x:Int,y:Int->x+y};
Copy the code

This is a standard way of writing a lambda expression, and because of type derivation in Kotlin, the form above can also be abbreviated as follows

 val function={x:Int,y:Int->x+y};
 val function:(Int,Int)->Int={x,y->x+y};
Copy the code

If the last argument in a higher-order function is a function argument, and the function argument takes the form of a lambda expression, it can be written as follows

  fun<T> isOdd(t:T,condition:(t:T)->Boolean):Boolean{
        return  condition(t);
    }

  val odd = isOdd(10){x:Int->x%2==0}
Copy the code

This is also one of kotlin’s recommended ways of writing in the Curryization style (The Curryization style is the process of converting a function that takes multiple arguments to one that takes a single argument, and then returns the next function before returning the final result). Examples of currization styles are as follows:

fun sum(x:Int,y:Int,z:Int)=x+y+z; The e (" result ", "${sum (1, 2, 3)}")Copy the code

The test results are as follows:

10-31 15:12:45. 052, 32386-32386 /? E/result: 6Copy the code

The Style of corrification is written as follows:

  fun sum(x:Int)={y:Int->{z:Int->x+y+z}}
  Log.e("result","${sum(1)(2)(3)}");
Copy the code

The test results are as follows:

10-31 15:17:51.845 32618-32618/com.zhqy.javademo E/result: 6
Copy the code

You can use this if you want a defined lambda expression to execute immediately

Var result = {x: Int, y: Int - > x + y} (1, 2);Copy the code

{x:Int,y:Int->x+y} creates an instance of a function that takes (1,2) arguments and returns 3

Anonymous parameters

An anonymous parameter is a function that does not have a specified function name. It is defined as follows:

val result=fun(x:Int,y:Int):Int{ return x+y; } (1, 2); Log.e("result","$result");Copy the code

The results are as follows:

10-31 15:23:23.645 923-923/com.zhqy.javademo E/result: 3
Copy the code

conclusion

That is all the content of this article, a brief introduction to the use of higher-order functions, lambda expressions, anonymous functions.