For the default function symbol, use:
(fn: (Option[V1], Option[V2]) => V3 = (x: Option[V1], y: Option[V2]) => Tuple2(x,y))
You will need to use the merge as follows: merge(m1,m2)()
I would say don't worry about performance until you take some actual data measurements.
Edit: about performance, providing an idea instead of building a map, you can quickly “build” through a search - provided that we are dealing with immutable maps. Thus, depending on the evidence and use cases, you may get better performance for certain operations, but it has a trade-off.
class MergedView[K, V1, V2, V3]( left: Map[K, V1], right: Map[K, V2] )(fn: (Option[V1], Option[V2]) => V3 = (x: Option[V1], y: Option[V2]) => Tuple2(x,y) ) extends collection.DefaultMap[K, V3] { def get(key: K): Option[V3] = (left.get(key), right.get(key)) match { case (None, None) => None case t => Some(fn(t._1, t._2)) } lazy val tuples = (left.keys ++ right.keys).map(key => key -> get(key).get) def iterator: Iterator[(K, V3)] = tuples.iterator } val r1 = new MergedView(m1, m2)() // use parens here for second param list.
source share