Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group, map & reduce with two different reducer-operators

I have these tuples:

("T1",2,"x1"),
("T1",2,"x2"),
// … etc

And i want to reduce it to ("T1", 4, List("x1", "x2")). How can i do this ?

I did something like .group(_._1).map{case (key,list) => key-> list.map(_._2).reduce(_+_)}
But this is not working, and just sums the numbers without appending the list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With groupMapReduce:

val xs = List(
  ("T1",40,"x1"),
  ("T1",2,"x2"),
  ("T2",58,"x3")
)

println(xs.groupMapReduce(_._1)
  (e => (e._2, List(e._3)))
  ({ case ((x, y), (z, w)) => (x + z, y ++ w)})
)

with groupBy:

val xs = List(
  ("T1",40,"x1"),
  ("T1",2,"x2"),
  ("T2",58,"x3")
)
println(xs.groupBy(_._1)
  .view
  .mapValues(ys => (ys.view.map(_._2).sum, ys.map(_._3)))
  .toMap
)

If you want to do it in one pass per list, and not use ++ you could try sth. like this:

xs.groupBy(_._1)
  .view
  .mapValues(ys =>
     ys.foldRight((0, List.empty[String])){
       case ((_, n, x), (sum, acc)) => (n + sum, x :: acc)
     }
  )
  .toMap

All three variants give

Map(T2 -> (58,List(x3)), T1 -> (42,List(x1, x2)))

Note that combining many lists with ++ might become very inefficient if the number of lists becomes large. It depends on your use-case whether this is acceptable or not.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading