1.
var a = 6
var c = if (a == 3) 4 else 5
val d = when (a) {
0 -> 5
1 -> 100
6 -> "666"
else -> 20
}
2.
val method: () -> Unit = {
println("hello")
}
val f1={ p:Int->
println("hello")
"Helo"
}
val date= f1(1)
3.
val fbmethod=fbmethod()
cost{
fbmethod()
}
fun cost(method:()->String){
val start=System.currentTimeMillis()
method()
println(System.currentTimeMillis()-start)
}
fun fbmethod():()->String{
return {
"@@Hello"
}
}
4.
inline fun method(haha:()->Unit){
}
5.
val person=Person("zhangsan",20)
person.let {it.name="lisi"}
person.run {name="lisi"}
var person2=person.also {it.name="lisi"}
var person3=person.apply {name="wangwu"}
File("heh.txt").inputStream().reader().buffered().use {
}
6.
val list = listOf<Int>(1, 2, 3, 4, 5)
list.asSequence().filter {
println("filter$it")
it % 2 == 0
}.map {
println("map$it")
it * 2 + 1
}.forEach {
println("foreach:$it")
}
list.flatMap {
0 until it
}.joinToString().let {
}
list.fold(StringBuilder()){
acc, i -> acc.append(i)
}