Lambdas and the Kotlin Standard Library

Now that you understand the fundamentals of how to declare and call functions that use lambdas, let’s dive deeper into the Kotlin standard library to see how they appear throughout the language. Update your main function with a call to require, which you saw for the first time in Chapter 7.

Listing 8.16  Remembering require (NyetHack.kt)

fun main() {
    narrate("A hero enters the town of Kronstadt. What is their name?") { message ->
        // Prints the message in yellow
        "\u001b[33;1m$message\u001b[0m"
    }
    val heroName = readLine() ?: ""

    val heroName = readLine()
    require(heroName != null && heroName.isNotEmpty()) {
        "The hero must have a name."
    } changeNarratorMood() narrate("$heroName heads to the town ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.