Reputation: 361
im looking for a way to merge maps. i can write code every time i need to merge maps by their keys but the problem is that the manipulation on the value is different all the time (for strings , lists of int)
is their a library for this issue ?
for example my input is :
//value is int - need to sum the values
val example1 = Map("a" -> 1 , "b" -> 1 , "c" -> 7)
val example2 = Map("a" -> 1 , "e" -> 5 , "f" -> 2)
//value is list - need to append
val example1 = Map("a" -> List(1) , "b" -> List(3) , "c" -> List(2))
val example2 = Map("a" -> List(4) , "e" -> List(1) , "f" -> List(1))
//value is string - nned to append
val example1 = Map("a" -> "asd" , "b" -> "efd" , "c" -> "sdf")
val example2 = Map("a" -> "ads" , "e" -> "sdfds" , "f" -> "czxc2")
Upvotes: 0
Views: 339
Reputation: 9913
I wrote a blog post about this , check it out :
http://www.nimrodstech.com/scala-map-merge/
basically using scalaz semi group you can achieve this pretty easily
would look something like :
import scalaz.Scalaz._
example1 |+| example2
Upvotes: 6