1, "b" -> 2), Map("a" -> 11, "c" -> 4), Map("b" ->...">

Merging maps using `aggregate`

For any given Map set, for example,

 val in = Array( Map("a" -> 1, "b" -> 2), Map("a" -> 11, "c" -> 4), Map("b" -> 7, "c" -> 10)) 

how to use aggregate on in.par to merge cards into

 Map ( "a" -> 12, "b" -> 9, "c" -> 14 ) 

Note Map Merge has been specified several times, but is looking for a solution with aggregate in parallel collections.

Many thanks

+5
source share
1 answer

How to apply merge to both seqop and comboop ?

 val in = Array( Map("a" -> 1, "b" -> 2), Map("a" -> 11, "c" -> 4), Map("b" -> 7, "c" -> 10) ) def merge(m1: Map[String, Int], m2: Map[String, Int]): Map[String, Int] = m1 ++ m2.map { case (k, v) => k -> (v + m1.getOrElse(k, 0)) } in.par.aggregate(Map[String, Int]())(merge, merge) 

Update

You go to the initial aggregate battery value (empty card) and two short circuits - seqop and comboop .

A parallel sequence is divided into several sections that need to be processed in parallel. Each section is processed by sequentially applying seqop to the battery and array element.

 def seqop( accumulator: Map[String, Int], element: Map[String, Int]): Map[String, Int] = merge(accumulator, element) 

seqop takes the initial value of the battery and the first element of the array and combines it. Then it takes the previous result and the next element of the array, and so on, until the entire section is merged on one map.

When each section is combined on a separate card, these cards should be combined using comboop . comboop takes the combined card from the first section and combines the card from the second section and combines it. Then he takes the previous result and the map from the third section, and so on, until everything is combined on one map. This is the result of aggregate .

 def comboop( m1: Map[String, Int], m2: Map[String, Int]): Map[String, Int] = merge(m1, m2) 

Just a coincidence: seqop and comboop match. In general, they differ in logic and signatures.

+1
source

Source: https://habr.com/ru/post/1200734/


All Articles