/**解构：通过componentN函数将一组值赋给一系列变量（a,b,c,d,e）,其主要定义在
 * 1.集合中,最多前5个元素可以解构
 * 2.data class中，其主构造器中的参数自动生成componentN函数
 * 3.普通class中，需手动将需结构的字段，编制componentN函数
 * 4.系统提供的二元组Pair和三元组Triple
 */
fun main() {
    //集合最多5个解构元素
    println("-----------------Collection Destruct------------------")
    val (x, y, z, w) = arrayOf(5, 10, 15,20)
    printAll("array destruct", x, y, z, w)
    val map = mapOf("Alice" to 21, "Bob" to 25)
    for ((name, age) in map) {
        println("map destruct:$name is $age years old")
    }
    val (min, max) = findMinMax(listOf(100, 90, 50, 98, 76, 83))
    printAll("list destruct", min, max)

    //自定义数据类,解构元素为主构造器上定义的参数
    println("-----------------Class Destruct------------------")
    data class User(val username: String, val email: String)
    fun getUser() = User("Mary", "mary@somewhere.com")
    val user = getUser()
    val (username, email) = user
    printAll("data class destruct", username, email)
    println(username == user.component1())
    val (_, emailAddress) = getUser()
    println(emailAddress == user.component2())

    //自定义普通类,解构元素为自定义componentN方法
    class Student(val name: String, val age: Int) {
        //componentN是操作符,重载它,必须添加operator修饰符
        operator fun component1(): String {
            return name
        }
        operator fun component2(): Int {
            return age
        }
    }
    val (studentName, studentAge) = Student("张三",13)
    printAll("class destruct", studentName, studentAge)

    //系统提供的两个人数据类：pair/triple
    println("-----------------Pair&Triple Destruct------------------")
    val (num, name) = Pair(1, "one")
    printAll("pair destruct:",num,name)
    val (number, englishName,chineseName) = Triple(1, "one","一")
    printAll("triple destruct:",number,englishName,chineseName)
}
//fun findMinMax(list: List<Int>):Pair<Int?,Int?>{
fun findMinMax(list: List<Int>):List<Int>{
    val min: Int? = list.min()
    val max: Int? = list.max()
    return listOf(min ?: 0, max ?: 0)
    //return Pair(min,max)
}




