Scala Map foreach

ScalaScala Collections

Scala Problem Overview


given:

val m = Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3)
m.foreach((key: String, value: Int) => println(">>> key=" + key + ", value=" + value))

why does the compiler complain

error: type mismatch
found   : (String, Int) => Unit
required: (String, Int) => ?

Scala Solutions


Solution 1 - Scala

I'm not sure about the error, but you can achieve what you want as follows:

m.foreach(p => println(">>> key=" + p._1 + ", value=" + p._2))

That is, foreach takes a function that takes a pair and returns Unit, not a function that takes two arguments: here, p has type (String, Int).

Another way to write it is:

m.foreach { case (key, value) => println(">>> key=" + key + ", value=" + value) }

In this case, the { case ... } block is a partial function.

Solution 2 - Scala

oops, read the doco wrong, map.foreach expects a function literal with a tuple argument!

so

m.foreach((e: (String, Int)) => println(e._1 + "=" + e._2))

works

Solution 3 - Scala

You need to patter-match on the Tuple2 argument to assign variables to its subparts key, value. You can do with very few changes:

m.foreach{ case (key: String, value: Int) => println(">>> key=" + key + ", value=" + value)} 

Solution 4 - Scala

The confusing error message is a compiler bug, which should be fixed in 2.9.2:

Solution 5 - Scala

Excellent question! Even when explicitly typing the foreach method, it still gives that very unclear compile error. There are ways around it, but I can't understand why this example does not work.

scala> m.foreach[Unit] {(key: String, value: Int) => println(">>> key=" + key + ", value=" + value)}
<console>:16: error: type mismatch;
 found   : (String, Int) => Unit
 required: (String, Int) => Unit
              m.foreach[Unit] {(key: String, value: Int) => println(">>> key=" + key + ", value=" + value)}
                                                         ^

Solution 6 - Scala

Docs says argument is tuple -> unit, so We can easily do this

Map(1 -> 1, 2 -> 2).foreach(tuple => println(tuple._1 +" " + tuple._2)))

Solution 7 - Scala

Yet another way:

Map(1 -> 1, 2 -> 2).foreach(((x: Int, y: Int) => ???).tupled)

However it requires explicit type annotations, so I prefer partial functions.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDzhuView Question on Stackoverflow
Solution 1 - ScalaPhilippeView Answer on Stackoverflow
Solution 2 - ScalaDzhuView Answer on Stackoverflow
Solution 3 - ScalaFrancois GView Answer on Stackoverflow
Solution 4 - ScalaPaul ButcherView Answer on Stackoverflow
Solution 5 - ScalaEishay SmithView Answer on Stackoverflow
Solution 6 - ScalavikashaitView Answer on Stackoverflow
Solution 7 - ScalaYaroslavView Answer on Stackoverflow