How to use the Fold Left Method in Scala?
The Fold Left method of the Scala programming language is used to perform any given calculation on a list or a collection starting from left to right. The main purpose of using this function instead of performing these calculations in a regular way is to prevent stack overflow errors while keeping the performance of the code optimized. For understanding the working of the Fold Left method of the Scala programming language in a better way, we have designed three different examples for you, which are discussed in detail below:
Example # 1: Using the Fold Left Method for Summing up an Array of Integers:
If you have an array and you want to find the sum of all of its elements. However, you do not wish to make your code lengthy by using a loop. In that case, you can conveniently use the Fold Left method of the Scala programming language for achieving the same functionality while writing an optimized code. The exact Scala script for such a program is as follows:
In this Scala program, we have a “FoldLeft” class inside which we have placed our “main()” function. Then, we have declared a list with the title array and have initialized it with five different integers. After that, we created an “output” value and assigned it the result of the “foldLeft” function. We have called this function with the name of our declared list and have assigned to it “0” as the initial value for calculating the sum. Finally, we have used the “println” statement for printing the value of the “output” variable on the terminal.
The compilation command shown below was used to compile this Scala script:
Then, the following execution command was used to run this Scala script:
You can see the sum of the given array in the image below, which implies that the “foldLeft” that we had used has worked exactly as we assumed.
Example # 2: Using the Fold Left Method for Summing up an Array of Floating Point Numbers:
In this example, we will share how you can calculate the sum of all the elements of an array with floating-point values. The following Scala script implements this functionality:
In this Scala Script, we have declared a list with the title “array” and assigned five different floating point values to it. Then, we have declared a value with the name “output” to which we have assigned the result of the “foldLeft” function. However, this time, we have passed “0.0” to this function as its starting value for the sum to notify this program that it is supposed to calculate the sum of different floating-point elements. Then, we have just used the “println” statement for displaying the result of this sum on the terminal.
The sum of all the floating-point elements of our list is shown in the image below, which is absolutely correct.
Example # 3: Using the Fold Left Method for Summing up an Array of Integers while making use of a Starting Point other than “0”:
Sometimes, you wish to calculate the sum of elements of any given array of numbers; however, you might not want to start calculating this sum from “0” i.e. you might want to add some other value to this sum by passing that value as a starting point of calculating that sum. For example, we have three numbers, “1”, “2”, and “3”. If we try to calculate their sum then it will turn out to be “6” if and only if the initial value of the sum is kept “0”. However, if the initial value of the sum will be something other than “0”, say “4”, then the sum of these numbers will turn out to be “10” instead of “6”. We can achieve the same functionality by using the “foldLeft” function of Scala as shown in the following script:
In this script, we have defined a list of integers that consists of five different integer values. Then, we have called the “foldLeft” function in the same manner as we did in the examples discussed above; however, this time, we have passed “25” as the starting value of this function instead of “0”. The “println” statement will be responsible for printing the output on the terminal.
Now, to see whether this will impact our output or not, we executed this Scala script. You can see in the image shown below that this time, the sum is different because its initial value was “25” instead of “0”.
This article was meant for elaborating the usage of the Fold Left function of the Scala programming language. We first gave you a slight gist of this method by explaining to you its basic purpose. After that, we implemented and shared with you three different examples that made use of the Fold Left function of the Scala programming language. By understanding these examples, you will quickly be able to learn the syntax of the Fold Left function of the Scala programming language. Moreover, you will also know how this function works and optimizes the overall performance of your code.