Higher-order Functions (part-II)
Learn about lambda expressions in Kotlin.
We'll cover the following...
Lambda expressions
In the previous lesson, we started by trying to understand the following line of code:
var lengths = strings.map {it.length}
But what about the strange curly braces?
To introduce the curly braces, we need to ‘inline’ our transformation function.
Like any other variable, we can inline getLengthOfString
by replacing that reference directly with its true value. In this case, this will be the transformation function itself. This is how it’s done:
Press + to interact
val lengths = strings.map(fun(str: String) : Int {return str.length})
In the snippet above, we replace the reference, getLengthOfString
, with its actual value.
However, since the function is anonymous anyway and can no longer be referenced from the outside, we can write it in an even ...