Question

I have a map:

Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3"))

I want to remove all None elements and flatten the map. What is the easiest way to accomplish that? I only found this way:

Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3")).filter(_._2.nonEmpty).map(item => (item._1 -> item._2.getOrElse(Nil)))

The result is:

Map(key1 -> value1, key3 -> value3)

Do you know a better way?

Was it helpful?

Solution

My take using pattern matching is:

Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3")).collect {
  case (key, Some(value)) => key -> value
}
// Map(key1 -> value1, key3 -> value3)

Collect acts like combined map + filter

OTHER TIPS

You can use for-comprehension + pattern-matching:

for((k, Some(v)) <- yourMap) yield k -> v

Using partition over the map, like this,

val (flattened,_) = map.partition(_._2.isDefined)

My take using for comprehensions:

val m = Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3"))
for( (key,value) <- m if(value.isDefined)) yield (key,value.get)

You could define the following helpers too, which allow for more compact syntax

implicit class RichPairedOptionIterableOps[A, B, Repr[_]](
    iterable: IterableOps[(A, Option[B]), Repr, Repr[(A, Option[B])]]
  ) {
    def collectWithSome: Repr[(A, B)] = iterable.collect { case (a, Some(b)) => a -> b }
    def collectWithNone: Repr[A] = iterable.collect { case (a, None) => a }
}

on your example:

Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3")).collectWithSome
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top