168: Learning Kotlin: Lambda Expressions Part 2

Download

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

167: Clean Architecture with Joe Birch

Download

Donn sits down with Buffer Android Lead, Joe Birch. Joe is a GDE for Android, Google Actions, Flutter and Google Pay. In this episode Donn and Joe talk about Clean Architecture, what it is, and why you might want to use it.

They break down the concept of what Clean Architecture is in a manner that is easy for even a beginner to understand.

Enjoy.

Shownotes

Get ahold of Joe:

Sponsors 🙏

Contact

166: Cross platform development talk with Jesse Wilson

Download

Kaushik decides to hit record on a skype call he has with friend of the show Jesse Wilson. They start off by discussing building features across different platforms today. Jesse talks about a clever mechanism of using javascript to change logic on the fly across the Square cash app, that’s worked out pretty well. They then go on to discussing how one can try and converge across platforms in terms of business logic, architecture, naming etc. They then wind it down by discussing the state of Flutter, Kotlin multiplatform and reaching the promised land.

Enjoy.

Shownotes

Sponsors 🙏

  • Instabug – Instabug is an SDK that completely takes care of your beta testing and user feedback process so you can debug, fix, and improve your app quality faster.

Go to instabug.com/fragmented, signup, install the SDK, and you will get their brand new t-shirt and a 14-day free trial.

Contact

165: Learning Kotlin: Lambda Expressions Part 1


Download

In this episode, Donn talks about Kotlin Lambda Expressions. He explains the syntax and how to build a couple of simple lambda expressions with and without type inference and declaration. We wrap up with a small example of passing a small lambda with multiple values to a function. See the show notes below for more info. This is part 1 of a multi-part series on Lambda Expressions in Kotlin.

The basic syntax of a lambda expression: kotlin
val myLambda : Type = { argumentList -> codeBody }

The codeBody is the only section that is not optional.

Double lambda expression (doubles an integer) with type inference kotlin
val double = { number: Int -> number * 2 }
val result = double(4)
// result = 8 now

Double string multi-line lambda with type inference. kotlin
val doubleString = { number: Int ->
// codebody
val doubleResult = number * 2
doubleResult.toString()
// Kotlin knows this will return a string
}

Type declaration in a lambda kotlin
val myLambda: (String, Int) -> String = { str, int ->
"$str - ${int.toString()}" // "Donn - 32"
}
val result = myLambda("Donn", 32)
// result = "Donn - 32"

Preview of next week … passing a lambda to a function `\kotlin fun doWork(name: String, favoriteNumber: Int, someLambda: (String, Int) -> String) { // Do some processing, this is a contrived example val repeatedString = “$name$name” val result = someLambda(repeatedString, favoriteNumber) println(result) }

// Usage doWork(“Donn”, 32) { str, int -> val someNewValue = “$str is my parameter and so is $int” someNewValue.length.toString() // this is returned }

// ’37’ is printed via println

// Or use it like this, the lambda code body is what can change, this is where the power is at doWork(“Donn”, 32) { name, count -> var result = “” for(i in 1..count) { result += “$name” } result // this is returned }

// loops over and concatinates “Donn” until the favorite number (aka count) is met. // Output looks like: “DonnDonnDonnDonnDonnDonn…” and so on…

`\

164: Learning Kotlin: Sealed Classes


Download

In this episode, you’ll learn all about Kotlin Sealed classes. Donn walks you through what they are, how to create them, how to use data classes, objects and regular classes to create a restricted type hierarchy.

Sponsors 🙏

Contact