Lambdas and Higher-Order Functions
Trailing Lambda
Move the final lambda argument outside the parentheses.
trailing-lambda
When a lambda is the last argument, Kotlin can place it after the call.
Trailing Lambda
TrailingLambda.kt
Replay: real traced execution (multi-file project)
fun buildSteps(count: Int, label: (Int) -> String): String {
var text = ""
for (index in 1..count) {
if (text != "") {
text += ","
}
text += label(index)
}
return text
}
fun main() {
val count = 3
val steps = buildSteps(count) { index ->
"step$index"
}
println("count=$count")
println("steps=$steps")
}
fun buildSteps(count: Int, label: (Int) -> String): String {
var text = ""
for (index in 1..count) {
if (text != "") {
text += ","
}
text += label(index)
}
return text
}
fun main() {
val count = 2
val steps = buildSteps(count) { index ->
"step$index"
}
println("count=$count")
println("steps=$steps")
}
fun buildSteps(count: Int, label: (Int) -> String): String {
var text = ""
for (index in 1..count) {
if (text != "") {
text += ","
}
text += label(index)
}
return text
}
fun main() {
val count = 4
val steps = buildSteps(count) { index ->
"step$index"
}
println("count=$count")
println("steps=$steps")
}
count ← 3
14fun main() {15 val count→ 3 = 3 //@count=2, 416 val steps = buildSteps(count3) { index ->17 "step$index"18 }text ← (empty)
1fun buildSteps(count3: Int, label: (Int) -> String): String {2 var text→ (empty) = ""text ← step1
pass 1 of 34for (index1 in 1..count3) {5 if (text != "") {6 text += ","7 }8 text→ step1 += label(index1)9}All 3 passes — pass 1 is the card above pass indextext1 1 (empty) → step1 2 2 step1 → step1, 3 3 step1,step2 → step1,step2, text ← step1,
pass 1 of 24for (index in 1..count) {5 if (textstep1 != "") {6 text→ step1, += ","7 }text ← step1,step2
7 }8 text→ step1,step2 += label(index2)9}text ← step1,step2,
pass 2 of 24for (index in 1..count) {5 if (textstep1,step2 != "") {6 text→ step1,step2, += ","7 }text ← step1,step2,step3
7 }8 text→ step1,step2,step3 += label(index3)9}return text
11 return textstep1,step2,step312}steps ← step1,step2,step3
15 val count = 3 //@count=2, 416 val steps→ step1,step2,step3 = buildSteps(count3) { index ->17 "step$index"18 }1920 println("count=$count3")21 println("steps=$stepsstep1,step2,step3")22}outputcount=3 steps=step1,step2,step3
count ← 2
14fun main() {15 val count→ 2 = 216 val steps = buildSteps(count2) { index ->17 "step$index"18 }text ← (empty)
1fun buildSteps(count2: Int, label: (Int) -> String): String {2 var text→ (empty) = ""text ← step1
pass 1 of 24for (index1 in 1..count2) {5 if (text != "") {6 text += ","7 }8 text→ step1 += label(index1)9}for (index in 1..count)
pass 2 of 24for (index2 in 1..count2) {5 if (text != "") {text ← step1,
4for (index in 1..count) {5 if (textstep1 != "") {6 text→ step1, += ","7 }text ← step1,step2
7 }8 text→ step1,step2 += label(index2)9}return text
11 return textstep1,step212}steps ← step1,step2
15 val count = 216 val steps→ step1,step2 = buildSteps(count2) { index ->17 "step$index"18 }1920 println("count=$count2")21 println("steps=$stepsstep1,step2")22}outputcount=2 steps=step1,step2
count ← 4
14fun main() {15 val count→ 4 = 416 val steps = buildSteps(count4) { index ->17 "step$index"18 }text ← (empty)
1fun buildSteps(count4: Int, label: (Int) -> String): String {2 var text→ (empty) = ""text ← step1
pass 1 of 44for (index1 in 1..count4) {5 if (text != "") {6 text += ","7 }8 text→ step1 += label(index1)9}All 4 passes — pass 1 is the card above pass indextext1 1 (empty) → step1 2 2 — 3 3 — 4 4 — text ← step1,
pass 1 of 34for (index in 1..count) {5 if (textstep1 != "") {6 text→ step1, += ","7 }All 3 passes — pass 1 is the card above pass text1 step1 → step1, 2 step1,step2 → step1,step2, 3 step1,step2,step3 → step1,step2,step3, text ← step1,step2
7 }8 text→ step1,step2 += label(index2)9}text ← step1,step2,step3
7 }8 text→ step1,step2,step3 += label(index3)9}text ← step1,step2,step3,step4
7 }8 text→ step1,step2,step3,step4 += label(index4)9}return text
11 return textstep1,step2,step3,step412}steps ← step1,step2,step3,step4
15 val count = 416 val steps→ step1,step2,step3,step4 = buildSteps(count4) { index ->17 "step$index"18 }1920 println("count=$count4")21 println("steps=$stepsstep1,step2,step3,step4")22}outputcount=4 steps=step1,step2,step3,step4