Skip to content

Instantly share code, notes, and snippets.

@john-kurkowski
Created May 14, 2011 10:37
Show Gist options
  • Save john-kurkowski/972103 to your computer and use it in GitHub Desktop.
Save john-kurkowski/972103 to your computer and use it in GitHub Desktop.
Scala tricks
scala> val m = Map(1 -> 2, 3 -> 4, 5 -> 6)
map: scala.collection.immutable.Map[Int,Int] = Map((1,2), (3,4), (5,6))
scala> val desiredKeys = Iterable(1, 2, 3)
desiredKeys: Iterable[Int] = List(1, 2, 3)
// Get several values out of a Map, or a default.
scala> desiredKeys map (m.getOrElse(_, -1))
res0: Iterable[Int] = List(2, -1, 4)
// Get several values out of a Map, if those keys are defined.
scala> desiredKeys collect m
res1: Iterable[Int] = List(2, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment