Write a function that receives another function.

higher-order A higher-order function takes a function argument or returns a function result.

Higher-Order Functions

score
HigherOrderFunctions.kt
Replay: real traced execution (multi-file project)
fun scoreWithBonus(score: Int, bonusRule: (Int) -> Int): Int {
    val bonus = bonusRule(score)
    return score + bonus
}

fun main() {
    val score = 10
    val total = scoreWithBonus(score) { current ->
        if (current >= 8) {
            5
        } else {
            2
        }
    }

    println("score=$score")
    println("total=$total")
}
fun scoreWithBonus(score: Int, bonusRule: (Int) -> Int): Int {
    val bonus = bonusRule(score)
    return score + bonus
}

fun main() {
    val score = 4
    val total = scoreWithBonus(score) { current ->
        if (current >= 8) {
            5
        } else {
            2
        }
    }

    println("score=$score")
    println("total=$total")
}
fun scoreWithBonus(score: Int, bonusRule: (Int) -> Int): Int {
    val bonus = bonusRule(score)
    return score + bonus
}

fun main() {
    val score = 8
    val total = scoreWithBonus(score) { current ->
        if (current >= 8) {
            5
        } else {
            2
        }
    }

    println("score=$score")
    println("total=$total")
}
  1. score ← 10

    6fun main() {7    val score→ 10 = 10 //@score=4, 88    val total = scoreWithBonus(score10) { current ->9        if (current >= 8) {10            511        } else {12            213        }14    }
  2. bonus ← 5

    1fun scoreWithBonus(score10: Int, bonusRule: (Int) -> Int): Int {2    val bonus→ 5 = bonusRule(score10)3    return score10 + bonus54}
  3. total ← 15

    7    val score = 10 //@score=4, 88    val total→ 15 = scoreWithBonus(score10) { current ->9        if (current >= 8) {10            511        } else {12            213        }14    }1516    println("score=$score10")17    println("total=$total15")18}
    outputscore=10
    total=15
  1. score ← 4

    6fun main() {7    val score→ 4 = 48    val total = scoreWithBonus(score4) { current ->9        if (current >= 8) {10            511        } else {12            213        }14    }
  2. bonus ← 2

    1fun scoreWithBonus(score4: Int, bonusRule: (Int) -> Int): Int {2    val bonus→ 2 = bonusRule(score4)3    return score4 + bonus24}
  3. total ← 6

    7    val score = 48    val total→ 6 = scoreWithBonus(score4) { current ->9        if (current >= 8) {10            511        } else {12            213        }14    }1516    println("score=$score4")17    println("total=$total6")18}
    outputscore=4
    total=6
  1. score ← 8

    6fun main() {7    val score→ 8 = 88    val total = scoreWithBonus(score8) { current ->9        if (current >= 8) {10            511        } else {12            213        }14    }
  2. bonus ← 5

    1fun scoreWithBonus(score8: Int, bonusRule: (Int) -> Int): Int {2    val bonus→ 5 = bonusRule(score8)3    return score8 + bonus54}
  3. total ← 13

    7    val score = 88    val total→ 13 = scoreWithBonus(score8) { current ->9        if (current >= 8) {10            511        } else {12            213        }14    }1516    println("score=$score8")17    println("total=$total13")18}
    outputscore=8
    total=13