Yield a sequence of tuples instead of map

北城余情 提交于 2019-12-11 11:10:06

问题


This is my code. To my surprise, it yields a map instead of a seq of tuples as I expect. What is right way to get list of tuples in scala?

for ((_, s) <- Constants.sites;
         line <- Source.fromFile(s"data/keywords/topkey$s.txt").getLines
    ) yield ((s, line))

回答1:


The reason probably is that Constants.sites is a Map, therefore it returns a map.

Instead of running the comprehension over Constants.sites, run it over Constants.sites.values, you are only using the values anyway.

The background is that your code gets translated to:

Constants.sites.flatMap {
  case (_, s) =>
    Source.fromFile(s"data/keywords/topkey$s.txt").getLines.map {
       line =>
         (s, line)
    }
}

And when calling flatMap on Map your resulting type also needs to be a Map, and the tuples can be coerced to a Map.

EDIT: But using this should be fine:

for {
  (_, s) <- Constants.sites
  line <- Source.fromFile(s"data/keywords/topkey$s.txt").getLines
) yield ((s, line))



回答2:


You could convert any Map to a Seq like this:

scala> val m = Map(1->"one", 2 -> "two")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> two)

scala> m.toSeq
res0: Seq[(Int, String)] = ArrayBuffer((1,one), (2,two))

In your case you might do

val result = for ((_, s) <- Constants.sites;
         line <- Source.fromFile(s"data/keywords/topkey$s.txt").getLines
    ) yield ((s, line))
result.toSeq


来源:https://stackoverflow.com/questions/30807104/yield-a-sequence-of-tuples-instead-of-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!