168: Learning Kotlin: Lambda Expressions Part 2
Publisher |
Spec
Media Type |
audio
Categories Via RSS |
Technology
Publication Date |
Jul 08, 2019
Episode Duration |
00:21:37

In this episode, Donn continues his talks about Kotlin Lambda Expressions. He explains how you can use lambda expressions as function parameters and as return types for functions.

This is a very dense episode - if you get lost look at the code snippets below or view on them on fragmentedpodcast.com


class LogReader {
    fun processFile(file: File, processLine: (String) -> Unit = {}) {
        file.forEachLine {
            println("Number of Chars: ${it.length}")
            processLine(it)
            println("Line Done Processing")
        }
    }

    fun processFileWithHandlers(file: File, logHandler: LogHandler) {
        file.forEachLine {
            println("Start of Processing")
            logHandler.handleLine().forEach { handler -> handler(it) }
            println("Line Done Processing")
        }
    }
}

interface LogHandler {
    fun handleLine(): List<(String) -> Unit>
}


val reader = LogReader()
val textFile = File("/Users/donnfelker/scratch/lorem.txt")

// Process with single lambda
reader.processFile(textFile, { println("First 10 Chars: ${it.substring(0..9)}") })

val logHandler = object : LogHandler {
    override fun handleLine(): List<(String) -> Unit> {
        return listOf<(String) -> Unit>(
            { line -> println("${line.substring(0, 1)}") },
            { line -> println("${line.substring(2, 4)}") },
            { line -> println("${line.substring(5, 10)}") }
        )
    }
}

// Process with multipe handlers via the logHandler
reader.processFileWithHandlers(textFile, logHandler)


Sponsors 🙏

Contact

In this episode, Donn continues his talks about Kotlin Lambda Expressions. He explains how you can use lambda expressions as function parameters and as return types for functions.

In this episode, Donn continues his talks about Kotlin Lambda Expressions. He explains how you can use lambda expressions as function parameters and as return types for functions.

This is a very dense episode - if you get lost look at the code snippets below or view on them on fragmentedpodcast.com


class LogReader {
    fun processFile(file: File, processLine: (String) -> Unit = {}) {
        file.forEachLine {
            println("Number of Chars: ${it.length}")
            processLine(it)
            println("Line Done Processing")
        }
    }

    fun processFileWithHandlers(file: File, logHandler: LogHandler) {
        file.forEachLine {
            println("Start of Processing")
            logHandler.handleLine().forEach { handler -> handler(it) }
            println("Line Done Processing")
        }
    }
}

interface LogHandler {
    fun handleLine(): List<(String) -> Unit>
}


val reader = LogReader()
val textFile = File("/Users/donnfelker/scratch/lorem.txt")

// Process with single lambda
reader.processFile(textFile, { println("First 10 Chars: ${it.substring(0..9)}") })

val logHandler = object : LogHandler {
    override fun handleLine(): List<(String) -> Unit> {
        return listOf<(String) -> Unit>(
            { line -> println("${line.substring(0, 1)}") },
            { line -> println("${line.substring(2, 4)}") },
            { line -> println("${line.substring(5, 10)}") }
        )
    }
}

// Process with multipe handlers via the logHandler
reader.processFileWithHandlers(textFile, logHandler)


Sponsors 🙏

Contact

This episode currently has no reviews.

Submit Review
This episode could use a review!

This episode could use a review! Have anything to say about it? Share your thoughts using the button below.

Submit Review