Updated June 23, 2023
Introduction to Scala reduce Function
Scala reduces functions to reduce the collection data structure in Scala. This function can be applied for both mutable and immutable collection data structures. Mutable objects are those whose values change frequently, whereas immutable objects are those objects that one assign cannot change itself. Reduce function can be applied to map, sequence, set, list, etc. This function returns a single value from the collection data structure. In reducing function, it merges all the values from the collection data structure and returns on a single value.
Syntax
Below is the syntax of Scala reduce:
def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1
Explanation: This is the syntax to define a reduced function in Scala as per their documentation. Ultimately, it will return a single value from the group of elements. Let’s see one practice example while de declares it into the program:
val result: Int = collection_name.reduce((a1, b1) => a1 + b1)
Explanation: In the above syntax, we have prepared one collection followed by the reduced function. It takes two parameters on which we can perform our operation. At last, we will obtain one value as output.
How does reduce Function work in Scala?
Reduce function reduces the number of elements in the collection data structure. What it does internally is it uses binary operation and merges all the available elements into one single element. We can see how it works internally. Suppose we have one list of elements like 3, 7, 9, 2, 1, 5. Now we will apply to reduce function to generate a single number after adding all the numbers from the list.
1. First, it will take the first element 3. Then it will apply a binary operation on the first and second numbers and merge them to generate the third number.
2. f(3, 7) -> 10; this will generate the third number using the binary operation.
3. f(10, 9) -> 19, not the output will be 19 after adding the next two-element.
4. f(19, 2) -> 21 the sum of the next two elements is 21 after applying the binary operation.
5. f(21, 1) -> 22, next two value sum is 22.
6. f(22, 5) -> 26; now the next two elements are 22 and 5. The merge value would be 26, and this is the last element from the list. So the last value is 26.
It will be generated as the output. In the reduce function, we use Anonymous function to iterate over the object into the list(=>). The operation of reduce function is associative and commutative.
Now we can have a look at one practical example for beginners for more understanding:
Code:
object Main extends App{
// Your code here!
// creating collection
val collection = List(200 , 400, 100, 30, 1000, 500)
// applying reduce function
val res = collection.reduce((x1, y1) => x1 max y1)
}
Explanation: In this example, we create one collection data structure list. After that, we apply to reduce function on the list to try to find out the max element from the list of elements. This will also return as a single value from the list of elements.
Extended class:
- AbstractIterable[(K, V)]
- Map[K, V]
Some of the supertype:
- Map
- Equals
- MapFactoryDefaults
- MapOps
- PartialFunction
- AbstractIterable
- Iterable
- IterableFactoryDefault
- IterableOps
- IterableOnceOps
- IterableOnceOps
Some known classes:
- TrieMap
- AbstractMap
- HashMap
- IntMap
- ListMap
- Longman
- Map1
- Map2
- Map3
- Map4
- withDefault
- TreeMap
- TrrSeqMap
- VectorMap
- AbstractMap
- AnyRefMap
- CollisionProofHashMap
- LinkedHashMap
- LongMap
- WithDefault
- SystemProperties
- ListMap
- OpenHashMap
Methods similar to reduce are followed:
- reduceLeft
- reduceRight
- reception
- reduceRightOPtion
- reduceLeftOPtion
Examples to Implement Scala reduce
Below are the examples mentioned :
Example #1
In this example, we find out the sum of all elements present in the collection data structure using a binary operation.
Code:
object Main extends App{
// Your code here!
// creating collection
val list1 = List(200 , 400, 100, 30, 1000, 500)
println("list before ::")
println(list1)
// applying reduce function
val result = list1.reduce((x1, y1) => x1 + y1)
println("list after ::")
println(result)
}
Output:
Example #2
In this example, we find out the subtraction of all elements present in the collection data structure using a binary operation.
Code:
object Main extends App{
// Your code here!
// creating collection
val list1 = List(200 , 400, 100, 30, 1000, 500)
println("list before ::")
println(list1)
// applying reduce function
val result = list1.reduce((x1, y1) => x1 - y1)
println("list after ::")
println(result)
}
Output:
Example #3
In this example, we are finding out the multiplication of all elements present into the collection data structure by using a binary operation.
Code:
object Main extends App{
// Your code here!
// creating collection
val list1 = List(200 , 400, 100, 30, 1000, 500)
println("list before ::")
println(list1)
// applying reduce function
val result = list1.reduce((x1, y1) => x1 * y1)
println("list after ::")
println(result)
}
Output:
Example #4
In this example, we find out the division of all elements present in the collection data structure using a binary operation.
Code:
object Main extends App{
// Your code here!
// creating collection
val list1 = List(200 , 400, 100, 30, 1000, 500)
println("list before ::")
println(list1)
// applying reduce function
val result = list1.reduce((x1, y1) => x1 / y1)
println("list after ::")
println(result)
}
Output:
Example #5
In this example, we are finding out the minimum of all elements present in the collection data structure by using a binary operation.
Code:
object Main extends App{
// Your code here!
// creating collection
val list1 = List(200 , 400, 100, 30, 1000, 500)
println("list before ::")
println(list1)
// applying reduce function
val result = list1.reduce((x1, y1) => x1 min y1)
println("list after ::")
println(result)
}
Output:
Conclusion
Scala reduces function is used to perform the binary operation on the collection elements. We can perform so many types of operations. Also, we can find the maximum and minimum values from the collection using the reduce function. Always remember this reduction will always return a single value as the output of the logic because it will merge all the values from the collection.
Recommended Articles
We hope that this EDUCBA information on “Scala reduce” was beneficial to you. You can view EDUCBA’s recommended articles for more information.