Fragmented - Android Developer Podcast

165: Learning Kotlin: Lambda Expressions Part 1

Informações:

Sinopse

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: val myLambda : Type = { argumentList -> codeBody } The codeBody is the only section that is not optional. Double lambda expression (doubles an integer) with type inference val double = { number: Int -> number * 2 } val result = double(4) // result = 8 now Double string multi-line lambda with type inference. val doubleString = { number: Int -> // codebody val doubleResult = number * 2 doubleResult.toString() // Kotlin knows this will return a string } Type declaration in a lambda val myLambda: (String, Int) -> String = { str, int -> "$str - ${int.toStri