Skip to content

Instantly share code, notes, and snippets.

@ioleo
Last active August 5, 2017 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ioleo/a3b2bd7662a68ae9d0e3ad9d31e5673e to your computer and use it in GitHub Desktop.
Save ioleo/a3b2bd7662a68ae9d0e3ad9d31e5673e to your computer and use it in GitHub Desktop.
DefinedValuesMapExtension
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