Kotlin

Kotlin Let

There are certain functions of the Kotlin programming language that are known as the scope definer functions, i.e., they define the scope of a particular variable or, in other words. To this extent, the value of a particular variable will be accessible. The “let” function of Kotlin also falls under this category of functions. In this article, we will try to learn more about this particular scope definer function of Kotlin in Ubuntu 20.04.

What is the Purpose of “Let” in Kotlin?

The “let” function of the Kotlin programming language is used to allow the execution of a code block about the context of an object. It means that as soon as you access that object outside that scope, you will be able to retrieve a different value than the one within the scope. Also, the “let” function of the Kotlin programming language is always used in conjunction with the “it” keyword, which is used to reference a particular object in any given situation. By using the Kotlin “let” function with the “it” keyword, you can easily manipulate the value of an object.

Using Kotlin Let:

For learning the usage of Kotlin “Let” in Ubuntu 20.04, you can take a look at the following three examples:

Example # 1: Printing a Greeting Message with Kotlin Let:

In this example, we will simply be printing a greeting message with Kotlin “Let” in Ubuntu 20.04. The Kotlin script shown in the image below will do the need full:

In this example script, we have defined a “greeting” variable within our “main()” function. Then, we have assigned to it a random string. After that, we have called the “let” function with the “greeting” variable. Inside this function, we want to append another string to the already existing value of the “greeting” variable and print it on the terminal. After that, we used another “println” statement to print the original value of the “greeting” variable on the terminal.

Now, we wanted to compile this script, because of which we have used the following command:

$ kotlinc let.kt

Then, for running the very same script, we have used the command shown below:

$ kotlin LetKt

The output of this Kotlin script is shown in the following image. You can see that the extended greeting message was printed on the terminal for the first time (because of using the “let” function). In contrast, for the second time (since the scope of the “it” keyword had terminated), only the original value of the “greeting” variable was printed on the terminal.

Example # 2: Printing a Greeting Message by converting it into Upper Case with Kotlin Let:

In this example, again, we will be printing a greeting message on the terminal with the Kotlin “let” function; however, with additional functionality, i.e., we will be converting the original string to upper case this time. For that, we have designed the Kotlin script shown in the image below:

In this Kotlin script, we have defined a “greeting” string inside our “main()” function. Afterward, we have assigned a random string to this variable while keeping all of its characters in lower case. This has been done intentionally because we will be converting this string to the upper case later. Then, we used a “println” statement to print the original message on the terminal. After that, we have defined another variable named “capitalize” and have assigned to it the statement “greeting.let {it.uppercase()}”. This statement will simply capitalize our “greeting” string and save this capitalized value to the “capitalize” variable. Finally, we have printed the value of the “capitalize” variable on the terminal.

The output of this Kotlin script is displayed in the following image. First, the greeting message in small alphabets was printed on the terminal, and then, the capitalized greeting message was printed on the terminal.

Example # 3: Filtering Out the Elements of a List with Kotlin Let:

Now, we will take you a step further with the help of this example. The functionality we want to achieve through this Kotlin script knows how many elements of a given list have lengths greater than specified. Moreover, we also want to print the lengths of all such elements on the terminal. For doing that, we have implemented the Kotlin script shown in the image below:

In this Kotlin script, we have created a value named “count” inside our “main()” function. Then, we have assigned a mutable list that consists of five different elements, i.e., One, Two, Three, Four, and Five. After that, we have used the statement “count.map {it.length}.filter {it > 3}.let {println(it)}”. This statement will only filter out those elements from our mutable list whose length will be greater than “3”. In our case, the third, fourth, and fifth elements of our list have lengths greater than three. Moreover, this statement will print their respective lengths on the terminal after filtering out these elements.

Upon executing this Kotlin script, the following output appeared on our terminal, i.e., 5, 4, 4. It means that the lengths of the three filtered out elements are “5”, “4”, and “4”, respectively. These lengths correspond to the following elements of our list: “Three,” “Four,” and “Five.” You can cross-check that our script managed to print out their lengths correctly.

Conclusion:

This article was meant to clear the probable ambiguities regarding using one of the scope definer functions of Kotlin, i.e., “let.” We explained the basic reason behind the existence of this function in Kotlin. This was followed by an explanation of three different examples in which we showed you varying situations in which you can use the Kotlin “let” function. These examples will help you employ this function to serve a useful purpose while working with the Kotlin programming language in Ubuntu 20.04.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.