Last active
August 5, 2017 21:42
-
-
Save ioleo/a3b2bd7662a68ae9d0e3ad9d31e5673e to your computer and use it in GitHub Desktop.
DefinedValuesMapExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object workspace { | |
implicit class DefinedValuesMapExtension[K >: Null, V](map: Map[K, Option[V]]) { | |
def toDefinedValuesMap: Map[K, V] = map.collect { case (key, value) if value.isDefined => (key, value.get) } | |
} | |
val mapWithOptionalValues: Map[String, Option[Int]] = Map( | |
"foo" -> Some(20), | |
"bar" -> None, | |
"baz" -> Some(100) | |
) | |
val mapWithDefinedValues = mapWithOptionalValues.toDefinedValuesMap | |
// defined class DefinedValuesMapExtension | |
// mapWithOptionalValues: Map[String,Option[Int]] = Map(foo -> Some(20), bar -> None, baz -> Some(100)) | |
// mapWithDefinedValues: Map[String,Int] = Map(foo -> 20, baz -> 100) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment