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:
Then, for running this executable file, we used the following command:
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:
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.