Scala | aggregate() Function
Last Updated :
17 Apr, 2019
The
aggregate() function is utilized to combine outcomes. Initially, a
sequence operation is applied as that is the first parameter of aggregate() function and then its followed by a
combine operation which is utilized to combine the solutions generated by the sequence operation performed. This function can be enforced on all the collection data structures in Scala and can be practiced on Scala's Mutable as well as Immutable collection data structures. It belongs to the
TraversableOnce trait in Scala.
Syntax:
def aggregate[B](z: => B)(seqop: (B, A) => B, combop: (B, B) => B): B
Where,
- B is the type of aggregated results, and z is the initial value for the aggregated result.
- seqop is an operator for sequence operation and is utilized to figure out the sum of each of the elements of the stated collection and also enumerates the total number of elements in the collection.
- combop is a combine operator which is utilized to combine the outcomes obtained by the parallel computation of the collection.
-
Parallel computation:
Let, List = (2, 3, 4, 5, 6, 7, 8)
Suppose, you have three threads of the list stated above where, let the first thread is (2, 3, 4), second thread is (5, 6) and the third thread is (7, 8).
Now, lets perform parallel computation.
-
First thread = (2, 3, 4) = (2+3+4, 3) = (9, 3)
// It is evaluated like below,
// (sum of all the elements, total number of elements)
Second thread = (5, 6) = (5+6, 2) = (11, 2)
Third thread = (7, 8) = (7+8, 2) = (15, 2)
Finally, after parallel computation, we have (9, 3), (11, 2), and (15, 2) and now this combine operator is applied to combine the outcomes of each thread i.e,
(9+11+15, 3+2+2) = (35, 7)
Now let's see an example.
Example:
Scala
// Scala program of aggregate()
// function
// Creating an object
object GfG
{
// Main method
def main(args: Array[String])
{
// Creating a list of numbers
val s = List(1, 2, 3, 4)
// Applying aggregate function
val r = s.par.aggregate((0, 0))((s, r) =>(s._1 + r, s._2 + 1),
(s,r) => (s._1 + r._1, s._2 + r._2))
// Displays summation of all the
// elements in the list and also
// total number of elements
println("(Sum of all the elements , total number of elements) = "+r)
}
}
Output:
(Sum of all the elements, total number of elements) = (10, 4)
Here,
par implies parallel which is utilized for the parallel computation of the list. we will discuss three parts in details.
aggregate(0, 0)
This is the first part where aggregate() function has two zeroes which are the initial values of the register
s so,
s._1 is at first zero which is utilized to figure out the sum of all the elements in the list and
s._2 is also zero in the beginning which helps in enumerating the total number of elements in the list.
(s._1 + r, s._2 + 1)
This is the second part, it performs the sequence operation for the list stated above. The first part of this code evaluates the sum and the second part is for counting total elements. Now, lets see the evaluation step by step.
Here, List = (1, 2, 3, 4)
(s._1 + r, s._2 + 1) // (Initially, s._1 and s._2 = 0)
= (0+1, 0+1) = (1, 1) // r is the initial value of the list
= (1+2, 1+1) = (3, 2)
= (3+3, 2+1) = (6, 3)
= (6+4, 3+1) = (10, 4)
This shows, how the evaluation is done exactly.
(s._1 + r._1, s._2 + r._2)
This is the last part, it is utilized in combine operation, as stated above in
parallel computation. Suppose, during parallel computation of the list(1, 2, 3, 4), it is broken into two threads i.e, (1, 2) and (3, 4) then lets evaluate it step by step.
First thread :
(1, 2) = (1+2, 2) = (3, 2)
Second thread :
(3, 4) = (3+4, 2) = (7, 2)
Now, lets combine the two threads i.e, (3, 2) and (7, 2) using combine operator as stated above.
(s._1 + r._1, s._2 + r._2) // s._1 and s._2 = 0
= (0+3, 0+2) = (3, 2) // r._1 = 3 and r._2 = 2
= (3+7, 2+2) = (10, 4) // r._1 = 7 and r._2 = 2
Thus, this part works like this.
Lets see, one more example.
Example:
Scala
// Scala program of aggregate()
// function
// Creating an object
object GfG
{
// Main method
def main(args: Array[String])
{
// Creating a sequence of strings
val seq = Seq("nidhi", "yes", "sonu", "Geeks")
// Applying aggregate function
val result = seq.par.aggregate(0)(_ + _.length, _ + _)
// Displays total number of
// letters used
println("The total number of letters used are: "+result)
}
}
Output:
The total number of letters used are: 17
Here, the initial value of the aggregate function is zero which is utilized to compute the total number of letters in the strings used here. The method
length is used to enumerate the length of each string.
Let's discuss the below code used in the above program in details.
(_ + _.length, _ + _)
Here, Seq = ("nidhi", "yes", "sonu", "Geeks")
Lets perform the sequence operation first.
(0 + "nidhi".length ) // (0+5) = 5
(0 + "yes".length) // (0+3) = 3
(0 + "sonu".length) // (0+4) = 4
(0 + "Geeks".length) // (0+5) = 5
Therefore, we have (5), (3), (4), (5) from the sequence operation.
Now, lets perform combine operation.
(5+3) = 8
(4+5) = 9
// Now lets combine it again
(8+9) = 17
Thus, total number of letters are 17.
Similar Reads
Scala | reduce() Function
The reduce() method is a higher-order function that takes all the elements in a collection (Array, List, etc) and combines them using a binary operation to produce a single value. It is necessary to make sure that operations are commutative and associative. Anonymous functions are passed as paramete
3 min read
How to create partition in scala?
In the world of big data, processing efficiency is key, and data partitioning emerges as a vital tool for optimizing performance. By strategically dividing large datasets into smaller subsets, partitioning enables parallel processing, significantly accelerating data manipulation tasks. In Scala, ach
2 min read
HashSet In Scala
HashSet is sealed class. It extends immutable Set and AbstractSet trait. Hash code is used to store elements. It neither sorts the elements nor maintains insertion order . The Set interface implemented by the HashSet class, backed by a hash table . In Scala, A concrete implementation of Set semantic
4 min read
Currying Functions in Scala with Examples
Currying in Scala is simply a technique or a process of transforming a function. This function takes multiple arguments into a function that takes single argument. It is applied widely in multiple functional languages. Syntax def function name(argument1, argument2) = operation Let's understand with
3 min read
How does Collect function Work in Scala?
The collect function in Scala is utilized to extract elements from a collection that satisfy a specified condition defined by a partial function. It can be invoked on any collection type, whether mutable or immutable and always returns a new collection containing the elements that meet the condition
3 min read
Recursion in Scala
Recursion is a method which breaks the problem into smaller sub problems and calls itself for each of the problems. That is, it simply means function calling itself. We can use recursion instead of loops. Recursion avoids mutable state associated with loops. Recursion is quite common in functional p
4 min read
BitSet in Scala
A set is a collection which only contains unique items which are not repeatable. A BitSet is a collection of small integers as the bits of a larger integer. Non negative integers sets which represented as array of variable-size of bits packed into 64-bit words is called BitSets. The largest number s
5 min read
HashMap in Scala
HashMap is a part of Scala Collection's. It is used to store element and return a map. A HashMap is a combination of key and value pairs which are stored using a Hash Table data structure. It provides the basic implementation of Map. Syntax: var hashMapName = HashMap("key1"->"value1", "key2"->"value
3 min read
Interesting fact about Scala
Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source
3 min read
Scala | Product3
Product3 is a trait in Scala, which is a Cartesian product of three elements. The Linear Supertypes here are Product, Equals, and Any and the known subclass here is Tuple3. It extends the trait Product i.e, trait Product3[+T1, +T2, +T3] extends Product Where, T1, T2, and T3 are the used parameter ty
2 min read