Getting values from Map given list of keys in Scala
Solution 1
Use flatMap:
ks flatMap { k => kv get k }
Or better:
ks flatMap (kv get)
And with for
comprehensions:
for (k <- ks; v <- kv get k) yield v
But none are as succinct or as clear as your own one-liner: :-)
ks map kv
List("k42") map kv // will barf
However, note that this is exceptional - it may throw an exception if ks
contains a key not defined in the map kv
.
Solution 2
Or:
ks collect kv
The result is composed of all the values of kv for which a key is in ks.
(Besides being a Function a Map is also a PartialFunction)
dmitry
Application developer. Team leader. Favourite languages are Scala, Python, Elixir, Ruby. Fluent in PHP, Java, Javascript, R, Matlab/Octave. Used a bit of Erlang, Haskell, Racket, Standard ML, C#. Somewhere back in time Delphi, C++, assemblies. Lover of matrices, big data, statictics, machine learning and neural networks.
Updated on June 13, 2022Comments
-
dmitry about 2 years
Basically I need to get a set of values from a map for a keys in a given list (in the same order keys are given).
val kv = Map("k3" -> "v3", "k1" -> "v1", "k2" -> "v2") val ks = List("k1", "k2")
The best I could have improvised is
foldRight
overks
:scala> (ks foldRight List[String]()) { (v, a) => kv(v) :: a } res7: List[String] = List(v1, v2)
Are there any more convenient ways to do this in standard lib or just the shorter code, ideally something alike
kv getVals ks
? :)Question is not so important of course, just trying to explore towards good style.
-
Core_Dumped about 10 yearsWouldn't
foldLeft
be a better option here? It would be a better style I think, because it is implemented by iteration while foldRight is implemented by recursion in List.scala.
-
-
dmitry over 11 yearsGreat,
collect
happens to be safe variant when keys might be missing. -
Faiz over 11 years+1 I think this is as good as it gets. How blind was I to not exploit
Map
being aPartialFunction
.