Abracadabra Lambda!!
Can you believe that you can actually pass functions to another function as a parameter? This is something that needs your attention. And this is really a core concept of Android’s JetpackCompose as I mentioned.
This is the fourth post of the Android Programming series. As I mentioned the last time, from this point on, I’ll keep posting about Kotlin almost nonstop. Since my current goal is to at least start building my Kotlin-powered Android app project, I don’t have much time left.
Without further adieu, let’s get into it!!
This time, we’ll cover Kotlin’s lambda. When I was coding in Java, I knew there was the same concept existed in the language, but I never had any chance to use it even at my work. I, however, knew that Android Studio has a special translation feature that converts a non-lambda expression into lambda(So, I’m kinda familiar with it). But at least in Kotlin, lambda appears to be more frequently used to make codes shorter and cleaner than in Java.
Given the aforementioned fact, lambda is something you may want to master if you also aim for an Android app developer position in the future.
Basics:
Here’s our first lambda-used source code. Just like every single time, this type of simple format doesn’t require any detailed description of the mechanism, right?
Actual code:
val sum: (Int, Int) -> Unit = {a,b -> println(a + b)}
fun main() {
sum(5,5)
}
And here’s the executed result.
The “it”:
Actual code:
val sum: (Int, Int) -> Unit = {a,b -> println(a + b)}
val calcLambda: (Int) -> Unit = {a -> println(a * 5)}
val calcLambdaIt: (Int) -> Unit = {it -> println(it * 5)}
fun main() {
sum(5,5)
calcLambda(5)
calcLambdaIt(10)
}
And here’s the executed result.
Unit:
Unit is basically Java;’s void, meaning it doesn’t return anything.
Actual code:
val helloWorld: (String) -> Unit = {it -> println("Hello" + " " + it)}
fun main() {
helloWorld("Android master")
}
And here’s the executed result.
Trailing Lambda:
Trailing Lambda allows you to pass functions as parameters to another function, and This is an important concept of JetpackCompose.
Okay, so I repeat what I said above because it’s important – can you believe that you can actually pass functions to another function as a parameter? This is something that needs your attention. And this is really a core concept of Android’s JetpackCompose as I mentioned.
Sample 1:
In the below source code (image 07), the add() function is passed to enhancedMessage() function as a parameter. Interesting, isn’t it?
val add: (Int, Int) -> Int = {a,b -> a + b}
fun main() {
enhancedMessage(message = "Hello, ") {
add(12, 12)
}
}
fun enhancedMessage(message: String, funAsParameter:() -> Int) {
println("$message ${funAsParameter()}")
}
Executed result:
Sample 2:
Here’s another sample that involved the trailing lambda in the previously posted code in Android Programming – Part 3 Kotlin Fundamentals Ⅲ: Boolean Functions.
Actual code:
fun waterCalc(a: IntArray): Int {
var waterLevel: Int
var totalAmount = 0
for (i in 0 until a.size - 1) {
var max_l = 0
var max_r = 0
var l = i
var r = i
val currentHeight = a[i]
while (r < a.size) {
if (a[r] > max_r) {
max_r = a[r]
}
r++
}
while (l >= 0) {
if (a[l] > max_l) {
max_l = a[l]
}
l--
}
waterLevel = Math.min(max_r, max_l) - currentHeight
if (waterLevel > 0) {
totalAmount += waterLevel
}
}
return totalAmount
}
fun enhancedMessage(message: String, funAsParameter:() -> Int) {
println("$message ${funAsParameter()}")
}
fun main() {
val a = intArrayOf(0, 1, 0, 2, 1, 0, 3, 1, 0, 1, 2)
enhancedMessage(message = "Hello, ") {
waterCalc(a)
}
}
Executed result:
Afterthoughts:
So, Kotlin is fun. This is really a fun language. Especially given how it takes advantage of the lambda expression, its kind of lego-stack-effect chemistry I see here provides us with a limitless level of excitement!