Kotlin parameter confusions, kotlin Parameter
The literal function is enclosed in braces and the parameter is declared before (the parameter type can be omitted). The function body is after->.
Parameter (T)-> R
/*** Map function maps a value to another value * map accepts a function, which has a parameter T */inline fun Array
. Map (transform :( T)-> R): List
{Return mapTo (ArrayList
(Size), transform)} val list = listOf (1, 2, 3, 4) val newList = list. map {it-> it % 2 = 0}/*** result: newlist = [false, true, false, true] * There is only one parameter, and the brackets can be omitted, the default it parameter */
The map function also accepts a function transform. The transform converts the T type in the map to R, and finally submits it to the mapto function for polling and execution of the transform. Finally, the entire set is converted into a map of R.
Parameter ()-> T
/*** ()-> T: a function of the T type is returned if no parameter exists */fun
F (body :()-> T ){}
(T, T)-> Boolean
/*** (T, T)-> Boolean: receives two T types and returns a Boolean value */fun max
(Collection: Collection
, Less: (T, T)-> Boolean): T? {Var max: T? = Null for (it in collection) // less is used as the function if (max = null | less (max !!, It) max = it return max}
The anonymous function has no name. Other syntaxes are similar to common functions.
var test3= fun(x:Int,y:Int):Int=x+yvar test4= fun(x:Int,y:Int):Int{ return x+y}