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

count
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")
}
  1. count ← 3

    14fun main() {15    val count→ 3 = 3 //@count=2, 416    val steps = buildSteps(count3) { index ->17        "step$index"18    }
  2. text ← (empty)

    1fun buildSteps(count3: Int, label: (Int) -> String): String {2    var text→ (empty) = ""
  3. text ← step1

    pass 1 of 3
    4for (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
    passindextext
    11(empty) step1
    22step1 step1,
    33step1,step2 step1,step2,
  4. text ← step1,

    pass 1 of 2
    4for (index in 1..count) {5    if (textstep1 != "") {6        text→ step1, += ","7    }
  5. text ← step1,step2

    7    }8    text→ step1,step2 += label(index2)9}
  6. text ← step1,step2,

    pass 2 of 2
    4for (index in 1..count) {5    if (textstep1,step2 != "") {6        text→ step1,step2, += ","7    }
  7. text ← step1,step2,step3

    7    }8    text→ step1,step2,step3 += label(index3)9}
  8. return text

    11    return textstep1,step2,step312}
  9. 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
  1. count ← 2

    14fun main() {15    val count→ 2 = 216    val steps = buildSteps(count2) { index ->17        "step$index"18    }
  2. text ← (empty)

    1fun buildSteps(count2: Int, label: (Int) -> String): String {2    var text→ (empty) = ""
  3. text ← step1

    pass 1 of 2
    4for (index1 in 1..count2) {5    if (text != "") {6        text += ","7    }8    text→ step1 += label(index1)9}
  4. for (index in 1..count)

    pass 2 of 2
    4for (index2 in 1..count2) {5    if (text != "") {
  5. text ← step1,

    4for (index in 1..count) {5    if (textstep1 != "") {6        text→ step1, += ","7    }
  6. text ← step1,step2

    7    }8    text→ step1,step2 += label(index2)9}
  7. return text

    11    return textstep1,step212}
  8. 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
  1. count ← 4

    14fun main() {15    val count→ 4 = 416    val steps = buildSteps(count4) { index ->17        "step$index"18    }
  2. text ← (empty)

    1fun buildSteps(count4: Int, label: (Int) -> String): String {2    var text→ (empty) = ""
  3. text ← step1

    pass 1 of 4
    4for (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
    passindextext
    11(empty) step1
    22
    33
    44
  4. text ← step1,

    pass 1 of 3
    4for (index in 1..count) {5    if (textstep1 != "") {6        text→ step1, += ","7    }
    All 3 passes — pass 1 is the card above
    passtext
    1step1 step1,
    2step1,step2 step1,step2,
    3step1,step2,step3 step1,step2,step3,
  5. text ← step1,step2

    7    }8    text→ step1,step2 += label(index2)9}
  6. text ← step1,step2,step3

    7    }8    text→ step1,step2,step3 += label(index3)9}
  7. text ← step1,step2,step3,step4

    7    }8    text→ step1,step2,step3,step4 += label(index4)9}
  8. return text

    11    return textstep1,step2,step3,step412}
  9. 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