score:0

Accepted answer

This combines the values pointwise, missing values for a key k are replaced by the identity element of the V-monoid:

def parMapMonoid[K, V](implicit vMon: Monoid[V]): Monoid[ParMap[K, V]] = {
  new Monoid[ParMap[K, V]] {
    def combine(v1: ParMap[K, V], v2: ParMap[K, V]): ParMap[K, V] = {
      val allKeys = v1.keys ++ v2.keys
      (for (k <- allKeys) yield {
        (k, vMon.combine(
          v1.getOrElse(k, vMon.empty),
          v2.getOrElse(k, vMon.empty)
        ))
      })(collection.breakOut)
    }
    def empty: ParMap[K, V] = {
      ParMap.empty[K, V]
    }
  }
}

Since the allKeys is also a parallel collection, the whole construction should still be "reasonably parallelized".

score:-1

If you need to just somehow combine ParMaps then you can just use regular Monoid[Map[_, _]] . No, unfortunately, you can't since ParMap is not a subtype of Map. But if you want to leverage ideomatic FP you should avoid structures like ParMap since they mix description of data and execution model.


Related Query

More Query from same tag