Scala: how to merge a collection of Maps

Well, you could do: mapList reduce (_ ++ _) except for the special requirement for collision. Since you do have that special requirement, perhaps the best would be doing something like this (2.8): def combine(m1: Map, m2: Map): Map = { val k1 = Set(m1.keysIterator.toList: _*) val k2 = Set(m2.keysIterator.toList: _*) val intersection = k1 … Read more

How to find duplicates in a list?

Try this: val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102) dup.groupBy(identity).collect { case (x, List(_,_,_*)) => x } The groupBy associates each distinct integer with a list of its occurrences. The collect is basically map where non-matching elements are ignored. The match pattern following case will match integers x that are associated with a list that fits the pattern … Read more

Why is zipped faster than zip in Scala?

None of the other answers mention the primary reason for the difference in speed, which is that the zipped version avoids 10,000 tuple allocations. As a couple of the other answers do note, the zip version involves an intermediate array, while the zipped version doesn’t, but allocating an array for 10,000 elements isn’t what makes … Read more

tech