Kotlin parameter confusions, kotlin Parameter

Source: Internet
Author: User

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}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.