Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Over the Keys/Values from an Option[Map]

Tags:

scala

I am trying to extract they keys/values from an Option[Map]. What is the simplest way to iterate over the key/values contains in the Map, only if the Option actually has a Map?

Here is a simple example that highlights my problem.

val values = Option(Map("foo" -> 22, "bar" -> 23))
values map { case (key, value) => println(s"$key = $value") }  

This fails to compile.

 <console>:12: error: constructor cannot be instantiated to expected type;
 found   : (T1, T2)
 required: scala.collection.immutable.Map[String,Int]
              values map { case (key, value) => println(s"$key = $value") }
                                ^

If the Map is not wrapped in an Option, then it works just fine.

val values = Map("foo" -> 22, "bar" -> 23)
values map { case (key, value) => println(s"$key = $value") }
like image 596
Nick Allen Avatar asked Jan 17 '26 02:01

Nick Allen


1 Answers

You need another map, because the first map is for the Option, which means the your lambda is trying to match a single key-value pair, when it's really the full Map contained in the Option.

values.map(a => ???)
           ^ This is a Map[String, Int]

Syntactically, you want this:

values.map(_.map { case (key, value) => println(s"$key = $value") })

But this isn't really a map in it's true sense, it's more like a foreach since it only produces a side-effect.

values.foreach(_.foreach { case (key, value) => println(s"$key = $value") })

Or with a for-comprehension:

for {
    map <- values
    (key, value) <- map
} println(s"$key = $value")
like image 191
Michael Zajac Avatar answered Jan 19 '26 17:01

Michael Zajac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!