36 lines
1004 B
Kotlin
36 lines
1004 B
Kotlin
fun main() {
|
|
listOf(1, 2, 3)
|
|
// Lambda, it is inferred argument, only used for single argument lambdas
|
|
.filter { it % 2 == 0 }
|
|
// Explicitly define args
|
|
.map { value -> value * value }
|
|
|
|
mapOf(1 to 2)
|
|
// Automatic destructuring of mep entries
|
|
.mapValues { (key, value) -> "$key $value" }
|
|
}
|
|
|
|
fun returnFromLambda(): List<Int> {
|
|
return listOf(3, 0, 5)
|
|
.flatMap {
|
|
if (it == 0) return emptyList() // Returns from the function, will always return an empty list when 0
|
|
listOf(it, it)
|
|
}
|
|
}
|
|
|
|
fun returnFromLambda2(): List<Int> {
|
|
return listOf(3, 0, 5)
|
|
.flatMap {
|
|
if (it == 0) return@flatMap emptyList() // Returns from the lambda
|
|
listOf(it, it)
|
|
}
|
|
}
|
|
|
|
fun returnFromLambda3(): List<Int> {
|
|
return listOf(3, 0, 5)
|
|
.flatMap l@{ // Define custom label
|
|
if (it == 0) return@l emptyList() // Returns from the lambda
|
|
listOf(it, it)
|
|
}
|
|
}
|