Possible Duplicate:
Better way of converting a Map[K, Option[V]] to a Map[K,V]
I have a Map[Symbol, Option[String]] from reading values from a web page, where some might be missing.
I’d like to ‘flatten’ this to Map[Symbol, String] removing all the None values.
The best I can do so far is
def removeNones[K, V](map: Map[K, Option[V]]): Map[K, V] =
map.collect { case kv if kv._2.isDefined => (kv._1, kv._2.get) }
but I really don’t like the case, and having to rebuild the Pair.
Can anyone find a nicer expression?
seems to do the trick.