Scala

Scala Flatmap

We have already talked about the usage of maps in Scala. Therefore, today, we will try to move one step further by introducing you to a very useful function of the Scala programming language, i.e., FlatMap. You might want to convert a different data structure to a map at times. Additionally, you might also want to flatten this map, i.e., separate out the elements of this map in the form of distinct characters. In this guide, we will be talking about the FlatMap method of the Scala programming language that will help us convert a given data structure to a map while flattening its output.

How to use the FlatMap Method in Scala?

For understanding how the Scala FlatMap method works, we will first show you a program that produces the same results as the FlatMap method would produce. After that, we will share another example program that uses the FlatMap method. By comparing the two programs and their outputs with each other, you will be able to see how beneficial it is to use the FlatMap method in Scala. This will be followed by yet another example in which we will be using the FlatMap method of Scala with a user-defined function. Now, let us get started by explaining all three of these examples.

Example # 1: Creating a Map from a List in Scala and Flattening its Output without using the FlatMap Method:

In this example, our goal is to implement a program that can create a map from a list by using the map method of Scala, and then we want to flatten the output of that map by using the flatten method of Scala. Notice that in the Scala script shown below, we will be using two different functions, i.e., map and flatten, to achieve the desired functionality.

We have created a list of names in this Scala script that contains three different names. We have also used the “println” statement for displaying these names on the terminal. Then, we have created a value named “output1” and assigned to it the result of the function “map.(_.toUpperCase)” that will not only convert the given list to a map but will also convert all of its alphabets to upper case. Then, we also wanted to print this output on the terminal. Lastly, we have created another value named “output2” and assigned to it the result of the flatten function. After doing this assignment, we used yet another “println” statement to print this output on the terminal.

To make an executable file for this Scala script, we used the command given below:

$ scalac FlatMap.Scala

Then, for running this executable file, we used the following command:

$ scala FlatMap

Now, it is time to discuss the output of this script which is shown in the image below. First, all the elements of the list were displayed on the terminal. Then, the result of the map method was displayed on the terminal, i.e., the elements of the list after getting converted to upper case in the form of a map. Finally, the result of the flatten method was displayed on the terminal, i.e., the elements of the map after removing their internal grouping were displayed in the form of distinct characters.

Example # 2: Creating a Map from a List in Scala and Flattening its Output with the FlatMap Method:

You can clearly see from the program shown above that to achieve the desired functionality; we had to run two distinct functions. Now, we will be presenting to you a Scala script that will produce the exact same output but more efficiently, i.e., by using lesser lines of code or, in other words, by using a single function instead of two.

We have declared a list of names in our Scala script in this example in the same manner as we did above. After printing the elements of this list on the terminal, we have created a value named “output”. Then, we have simply assigned to it the result of the FlatMap function after converting the input to upper case with the statement “val output=names.flatMap(_.toUpperCase)”. Finally, we have printed this whole result on the terminal.

You can clearly see from the following output that the final result produced in this example is exactly the same as our first example. The only difference is that we have managed to bypass an additional step in which the list was first converted to a map, and its elements were also changed to the upper case rather this time, everything took place at once.

Example # 3: Using the Scala FlatMap Method with a User-defined Function:

By now, you would have clearly understood why we used the FlatMap method instead of using the map and flatten methods separately. Now, we will simply share another example for bringing more clarity to the usage of the FlatMap function.

In this script, we have first declared a list of three numbers. After printing these numbers on the terminal, we have created a value named “output” and have assigned to it the result of a user-defined function that simply multiplies each element of the given list with “2” and generates a map with it by using the FlatMap function. Then, we used the “println” statement to display this output on the terminal.

The following image shows the results of this Scala script on the terminal:

Conclusion:

This article introduced you to the FlatMap method of the Scala programming language. We first taught you the significance of this method by sharing an example that does not use this method. After that, we highlighted the usage of this method by implementing another example that uses this method. Finally, to elaborate this usage more, we shared yet another example of this method along with a user-defined function. By going through this tutorial, you will instantly understand why the FlatMap method is used in the Scala programming language.

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.