Scala

Scala Lambda

The Scala programming language is quite diverse as it holds numerous functions to perform various operations. One of its unique functions is Lambda, which is known as an anonymous function and can be used to perform several different calculations by reducing the lines of code. The lambda function can be altered according to a user’s needs and used on variables, lists, and different date collections. If you are unfamiliar to the use of lambda in Scala programming, this article will increase your expertise in Scala programming, i.e., it will discuss different ways to use lambda.

Example 01

So, we will start our first Scala programming by creating a new Scala file in Linux and opening it in the text editor to edit. We have initiated this program with an object “test” containing the main function definition to start its execution. We have been using the “val” keyword to initialize two variables, v1 and v2, with the help of a lambda expression.

The variable v2 will contain a value of integer type by a variable “x” by adding the number 6. In contrast, the variable v2 will contain the sum of two integer numbers, “x” and “y”. The println() statements use variables v1 and v2 as a function call to lambda expressions by passing values 7 to v1 and 7 and 6 to v2. Let’s save this code before compilation.

You can see that the test.scala file is already listed, and we tried the scalac compiler to compile it. The object file has been created with its object name “test”, and we have tried the Scala query to run the test object file. The output shows the number 13 as the result of using lambda expressions for both values v1 and v2 differently.

Example 02

The lambda expressions can be applied to the list structures of Scala programming. So, we have implemented this example to apply lambda expressions on lists by starting it with the object “test”. The main() function definition started with the initialization of two lists list1, and list2, by using the List() function. Each list contains 5 integer type values.

Here comes the lambda expression that has been getting the value “z” from the calling function, calculates its square, and saves it to another value, “r”. Now, this lambda expression value “r” would be used in the map() function to be applied on both the lists separately, and the result would be saved to the r1 and r2 variables. The println() function statement would use the variables r1 and r2 that are being mapped with the lambda expression to display the results. Let’s save this code and go back to the shell for compilation.

After compiling the main code file with the scalac compiler and running its object file with the Scala instruction, we have completely different lists compared to the lists we had at the start. We have used the lambda expression to find the square of each separate value of each list.

Example 03

Let’s look at another example of Scala to use the lambda expression differently. We have been starting this example with the same test object containing a definition of the transform function that will use the lambda expression to calculate the multiplication result of an integer and a float number. The value passed to the transform function as an integer would be taken as a float.

The main() function definition has been started with the use of a transform function call using the lambda expression containing a value 7 passed to the variable “v” to calculate the multiplication result and save the result to a new variable “z”. The println() function statement has used the variable “z” to be displayed on the console.

We have compiled and executed this code’s object file after saving it. The following image illustrates the duplicate format of the result we received:

Similarly, the same lambda expression can be applied to the list after using the map() function, i.e., to apply the lambda expression on each value of a list. So, we started the code with the main() function calling a transform function by passing it a list along with the lambda expression to be calculated. The returned result would be saved to the variable “z”.

The integer list has been successfully modified to the double type list as displayed.

Example 04

Let’s take a look at the last lambda example of this article. It started with the main() function definition initializing a list “l” of string type holding a total of 5 string elements. The println() statement shows that we display the list before using the lambda expression. Then, the println() statement uses the “l” list for display. The variable “v” has been using the lambda expression to get the “x” string from the list and concatenate the special character “*” at its start and end.

The variable “r” is using the map() function to map the lambda expression “v” on the list “l”. The returned result would be saved to “r” and printed out at the console via the println() statement after using the lambda expression.

We have compiled and executed this code example. The list before the lambda expression was displayed first, and then the updated list was displayed.

Conclusion

This article discussed how we make use of the lambda expression in Scala programming to perform unique operations on different types of data structures, i.e., variables, lists, etc. We have started this guide with the simple lambda example using integer variables, followed by another example using the lambda expression for lists. The map() function has been utilized in the codes as well for mapping. In the last example, we tried the lambda expression to concatenate some specific characters at the start and end of each element of a list.

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.