下面的代码片段被认为是给我带来Map[String, (String, Int)]
的哈希图。
def genList(xx: String) = {
Seq("one", "two", "three", "four")
}
val oriwords = Set("hello", "how", "are", "you")
val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2
genList(currentWord).map(ps => {
val src = cmap get ps
if(src == None) {
cmap + (ps -> (w, xv))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> (w, xv))
}
else cmap
}
})
}
)
但是我遇到以下错误:
error: too many arguments for method ->: (y: B)(String, B)
cmap + (ps -> (w, xv))
^
更新:使用答案中提到的建议更改,以上错误已消除。
val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2
genList(currentWord).map(ps => {
val src = cmap get ps
if(src == None) {
cmap + (ps -> ((currentWord, xv)))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> ((currentWord, xv)))
}
else cmap
}
})
}
)
但是现在上面的代码出现了新的错误,如下所示:
error: type mismatch;
found : Seq[scala.collection.immutable.Map[String,(String, Int)]]
required: scala.collection.immutable.Map[String,(String, Int)]
genList(currentWord).map(ps => {
^
答案 0 :(得分:2)
ps -> (w, xv)
被解释为
ps.->(w, xv)
即传递两个参数而不是您的预期,而是传递一个2元组作为单个参数:
ps.->((w, xv))
或使用运算符语法:
ps -> ((w, xv))
答案 1 :(得分:1)
您需要添加一个额外的括号,因为单个括号被解释为方法应用程序:
cmap + (ps -> ((w, xv)))
这意味着:
cmap + (ps.->((w, xv)))
或者您可以两次使用->
:
cmap + (ps -> (w -> xv))