kotlin函数

fun calculate(x:Int,y:Int,operation:(Int,Int)->Int):Int
{
    return operation(x,y)
}
fun sum(x:Int,y:Int) = x + y
fun mul(x:Int,y:Int) = x*y
fun operation():(Int)->Int
{
    return ::square
}
fun square(x:Int) = x*x
data class Item(val name:String,val price:Float)
data class Order(val items:Collection)

fun Order.maxPricedItemValue():Float = this.items.maxByOrNull { it.price }?.price ?: 0F
fun Order.maxPricedItemName() = this.items.maxByOrNull{it.price}?.name ?:"NO_PRODUCTS"
val Order.commaDelimitedItemNames:String
    get() = items.map{it.name}.joinToString()

fun main(args: Array) {
    val order = Order(listOf(Item("Bread",25.0f),Item("Wine",29.0f),Item("Water",12.0f)))

    println("Max priced item name: ${order.maxPricedItemName()}")
    println("Max priced item value: ${order.maxPricedItemValue()}")
    println("Items:${order.commaDelimitedItemNames}")
//    val upperCase1:(String) -> String = {str:String -> str.uppercase()}
//    val upperCase2:(String) -> String = {str -> str.uppercase()}
//    val upperCase3 = {str:String -> str.uppercase()}
    val upperCase4 = {str -> str.uppercase()}
//    val upperCase5:(String) -> String = {it.uppercase()}
//    val upperCase6:(String) -> String = String::uppercase
//
//    println(upperCase1("hello"))
//    println(upperCase2("hello"))
//    println(upperCase3("hello"))
//    println(upperCase5("hello"))
//    println(upperCase6("hello"))
//    val func = operation()
//    println(func(2))

//    val sumResult = calculate(4,5,::sum)
//    val mulResult = calculate(4,5,::mul)
//    val mulResult1 = calculate(4,5){a , b-> a*b}
//    println("sumResult $sumResult, mulResult $mulResult")

}

你可能感兴趣的:(kotlin,开发语言,android)