Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, you could do:</p> <pre><code>mapList reduce (_ ++ _) </code></pre> <p>except for the special requirement for collision.</p> <p>Since you do have that special requirement, perhaps the best would be doing something like this (2.8):</p> <pre><code>def combine(m1: Map, m2: Map): Map = { val k1 = Set(m1.keysIterator.toList: _*) val k2 = Set(m2.keysIterator.toList: _*) val intersection = k1 &amp; k2 val r1 = for(key &lt;- intersection) yield (key -&gt; (m1(key) + m2(key))) val r2 = m1.filterKeys(!intersection.contains(_)) ++ m2.filterKeys(!intersection.contains(_)) r2 ++ r1 } </code></pre> <p>You can then add this method to the map class through the Pimp My Library pattern, and use it in the original example instead of "<code>++</code>":</p> <pre><code>class CombiningMap(m1: Map[Symbol, Double]) { def combine(m2: Map[Symbol, Double]) = { val k1 = Set(m1.keysIterator.toList: _*) val k2 = Set(m2.keysIterator.toList: _*) val intersection = k1 &amp; k2 val r1 = for(key &lt;- intersection) yield (key -&gt; (m1(key) + m2(key))) val r2 = m1.filterKeys(!intersection.contains(_)) ++ m2.filterKeys(!intersection.contains(_)) r2 ++ r1 } } // Then use this: implicit def toCombining(m: Map[Symbol, Double]) = new CombiningMap(m) // And finish with: mapList reduce (_ combine _) </code></pre> <p>While this was written in 2.8, so <code>keysIterator</code> becomes <code>keys</code> for 2.7, <code>filterKeys</code> might need to be written in terms of <code>filter</code> and <code>map</code>, <code>&amp;</code> becomes <code>**</code>, and so on, it shouldn't be too different.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload