What is Reduce function in Scala?
In general, the Scala reduce function is used to apply binary operations of each element of a collection. The Scala reduce function takes only associative and commutative binary functions as a parameter then applies that to the elements of the collection. These collections arrangements contain lists, sets, maps, sequences, and tuples.
Syntax of the Reduce function in Scala:
The syntax of Scala reduces function is as per their documentation, which looks this way.
The Scala reduce function first takes two parameters from the elements of a given list, in which we can perform our operations. “op” parameter is for binary operations, which merges all the elements for collection data structures and returns a single value. The sign “=>” is a transformer that is used to perform the list parameter of an anonymous function from the left-hand side into a right-hand side using the expression present there.
In the end, the Scala reduce function returns a single value from the combined collection of elements performed using binary operations.
How to use Reduce in Scala in Ubuntu 20.04:
Let’s take an example and see how Scala reduces function results when we apply this function on elements of collection data structures.
Example # 1: Using the Reduce function in Scala to find the sum.
In the first example of the Scala reduce function, we find the sum of all elements in the data structure collection that will perform binary operation addition.
Here in the code of example 1, we have a class as “sum1”, and all the code is implemented in the main function of the class. In the main function, we have a variable with the keyword “val” that represents “L1” which takes the input values of the list. By using Scala reduce function in the second variable as naming “output” and in that we have an anonymous function that is performing the binary operation of addition. We have a single value from the sum of “L1” elements. Note that at first, it will take the first element “23” from the list and then the second element “24”. From there, we will get a third number which is a sum of these first and second elements. In the same way, it goes on iterating over the elements into the list(=>).
Example # 2: Using the Reduce function in Scala to find the Min and Max value.
We have another example of using the Scala reduce function that will perform a binary operation to find the minimum and maximum value from the element of list collection.
The main function of class “number ” has a variable “E1” which takes five different numeric values as an input and calls in our second variable “E2” by using the Scala reduce function to perform maximum on the elements of the list collection. The other variable, “E3” will find the Min value from the “E1” list element through the Scala reduce function. At last, we have print statements that will show the new min and max element values.
The output of min and max values is displayed on the console screen of Ubuntu 20.04.
Example # 3: Using the Reduce function with the Map function to find the Average value.
The example of finding the average of the list elements by using Scala reduce function and another function called Scala map function. This Scala map function allows us to transform a collection of list elements into another collection of elements. On the other hand, the Scala reduce function performs the binary operation in the collection. Let’s execute an example code.
The Class “math” defines our main function implementation in the above code. Here, we have a variable “Val” as “M1” that has the first list elements having different numbers. Then we have another variable, “M2” that will convert every element of “M1” to a pair from (a, 1) using the Scala map function as “1” which is the initial frequency counter of all elements. The result is shown in the variable “result” in which we are adding all the elements at the corresponding position. Now print the result to get a list with two elements. The first element is the sum, and the other element is the number of that particular element. We have another printing statement that gives an Average from the result value and sets the data type to float.
The output after implementation and execution from the above code is below in the screenshot.
Example # 4: Using the Reduce function in Scala for Strings:
All the above examples are for integer values. We are introducing string values in Scala reduce function that will concatenate the strings into a single value. Let’s have an example code of how it works.
The above code example of strings has a class declared as “strings” that takes strings elements in the main function. The variables “str1″,”str2”,”str3”, and “str4” are initialized with different string elements of the list. Then in the variables “strResult1″,”strResult2″,”strResult3”, and “strResult4”, we are using the Scala reduce function that will concatenate the string elements into a single string. Through the print statement, you can see how it will generate a new string.
Displaying the output string below in the code.
At last, we have discussed how Scala’s built-in reduce function will use binary operation of the collection elements and modify it into a single value. We can perform many operations using the Scala reduce function. In this article, we have discussed a few code examples that show how we can find the sum of the elements, max value, min value, and use map function with Scala reduce function. This guide will help you understand the fundamentals of using Scala to reduce function.