Leverage Kotlin Functions & Lambdas

May 22 2024 · Kotlin 1.9.24, Android 14, Kotlin Playground

Lesson 03: Use Lambdas

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll create a lambda function and learn how to call it. To code along, open Kotlin Playground from your browser. Let’s get going.

val stringLambda = { myString: String ->
  val stringLength = myString.length
  if (stringLength > 15) {
    println("A very long string")
  } else {
    println(myString)
  }
}
fun main() {
 stringLambda("A quick brown fox jumped over a lazy dog!")
}
val stringLambda = { myString: String ->
  val stringLength = myString.length
  stringLength * 100
}
fun main() {
  val newLength = stringLambda("A quick brown fox jumped over a lazy dog!")
  println(newLength)
}
val stringSum: (Int, Int) -> String = { num1: Int, num2: Int ->
  val sum = (num1 + num2)
  sum.toString()
}
fun main() {
  val result = stringSum(100, 30000)
  println("$result")
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion