Created
May 14, 2011 10:37
-
-
Save john-kurkowski/972103 to your computer and use it in GitHub Desktop.
Scala tricks
This file contains hidden or 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
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