scala Scala错误“值映射不是Double的成员”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34984070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala error "value map is not a member of Double"
提问by JimLohse
Explanation: I accepted gzm0'sanswer because it rocked!
说明:我接受了gzm0 的回答,因为它震撼了!
@Eduardo did come in with a comment suggesting:
@Eduardo 确实发表了评论建议:
(for(i <- 10..20; j=runTest(i)) yield i -> j).toMap
which also lets me run build, he just never posted an answer and @gzm0 answer was conceptually AWESOME so I accepted it.
这也让我运行构建,他只是从未发布过答案,@gzm0 的答案在概念上非常棒,所以我接受了。
Once I get this other issue figured out relating to "can't call constructor"I will be able to test these out by actually running the program LOL
一旦我弄清楚与“无法调用构造函数”有关的另一个问题,我将能够通过实际运行程序来测试这些 LOL
Question: I have an error in this expression, specifically how to fix it but more generally what am I missing about FP or Scala to make this mistake?
问题:我在这个表达式中有一个错误,特别是如何修复它,但更一般地说,我在 FP 或 Scala 中遗漏了什么导致这个错误?
timingsMap = for (i <- powersList; j <- runTest(i)) yield i -> j
I am Writing my first Gradle/Scala project for a Analysis of Algorithms assignment. Scala is not part of the assignment so I am not using a homework tag. Except for my work with Spark in Java, I am brand new to Functional Programming I am sure that's the problem.
我正在为算法分析作业编写我的第一个 Gradle/Scala 项目。Scala 不是作业的一部分,所以我没有使用作业标签。除了我在 Java 中使用 Spark 的工作之外,我是函数式编程的新手,我确信这就是问题所在。
Here's a snippet, the full .scala file is on GitHub, is that OK or I will post the full program here if I get flailed :)
这是一个片段,完整的 .scala 文件在 GitHub 上,可以吗,否则我会在此处发布完整的程序,如果我遇到麻烦:)
val powersList = List(10 to 20)
// create a map to keep track of power of two and resulting timings
var timingsMap: Map[Integer, Double] = Map()
// call the runTest function once for each power of two we want, from powersList,
// assign timingsMap the power of 2 value and results of runTest for that tree size
timingsMap = for (i <- powersList; j <- runTest(i)) yield i -> j
The error is: /home/jim/workspace/Scala/RedBlackTree4150/src/main/scala/Main.scala:36: value map is not a member of Double timingsMap = for (i <- powersList; j <- runTest(i)) yield i -> j
错误是:/home/jim/workspace/Scala/RedBlackTree4150/src/main/scala/Main.scala:36: value map is not a member of Double TimingsMap = for (i <- powersList; j <- runTest(i )) 产量 i -> j
What I thinkI am doing in that timingsMap = ...line is get all the elements of powersListmapped onto ifor each iteration of the loop, and the return value for runTest(i)mapped onto jfor each iteration, and then taking all those pairs and putting them into timingsMap. Is it the way I am trying to use iin the loop to call runTest(i)that causes the problem?
我认为我在该timingsMap = ...行中所做的是为循环的每次迭代获取powersList映射到的所有元素i,以及为每次迭代runTest(i)映射到的返回值j,然后将所有这些对放入timingsMap. 是我试图i在循环中使用的方式call runTest(i)导致问题吗?
runTest looks like this:
runTest 看起来像这样:
def runTest(powerOfTwo: Range): Double = {
// create the tree here
var tree = new TreeMap[Int, Double]
// we only care to create a tree with integer keys, not what the value is
for (x <- powerOfTwo) {
// set next entry in map to key, random number
tree += (x -> math.random)
}
stopWatchInst.start()
// now go through and look up all the values in the tree,
for (x <- powerOfTwo) {
// get the value, don't take the time/overhead to store it in a var, as we don't need it
tree.get(x)
}
// stop watch check time report and return time
stopWatchInst.stop()
val totalTime = stopWatchInst.elapsed(TimeUnit.MILLISECONDS)
loggerInst.info("run for 2 to the power of " + powerOfTwo + " took " + totalTime + " ms")
return totalTime
}
Note: I've had a suggestions to change the j <-to =in j <-in this line: timingsMap = for (i <- powersList; j <- runTest(i)) yield i -> j
注意:我有一个建议来改变这一行中的j <-to =in j <-:timingsMap = for (i <- powersList; j <- runTest(i)) yield i -> j
Another suggestion didn't like using yield at all and suggested replacing with (10 to 20).map...
另一个建议根本不喜欢使用 yield 并建议替换为 (10 to 20).map ...
The strange part is the existing code does not show an error in the IntelliJ editor, only breaks when I run it. The suggestions all give type mismatch errors in the IDE. I am really trying to figure out conceptuallywhat I am doing wrong, thanks for any help! (and of course I need to get it to work!)
奇怪的是,现有代码在 IntelliJ 编辑器中没有显示错误,只有在我运行时才会中断。这些建议都会在 IDE 中给出类型不匹配错误。我真的想从概念上弄清楚我做错了什么,感谢您的帮助!(当然我需要让它工作!)
After trying gzm0 answer I am getting down the same road ... my code as presented doesn't show any type mismatches until I use gradle run... whereas when I make the suggested changes it starts to give me errors right in the IDE ... but keep em coming! Here's the latest error based on gzm0s answer:
在尝试 gzm0 answer 后,我走上了同样的道路......我所提供的代码在我使用之前没有显示任何类型不匹配gradle run......而当我进行建议的更改时,它开始在 IDE 中给我错误.. . 但让他们来!这是基于 gzm0s 答案的最新错误:
/home/jim/workspace/Scala/RedBlackTree4150/src/main/scala/Main.scala:37: type mismatch;
found : List[(scala.collection.immutable.Range.Inclusive, Double)]
required: Map[Integer,Double]
timingsMap = for (i <- powersList) yield i -> runTest(i)
回答by gzm0
You want:
你要:
for (i <- powersList)
yield i -> runTest(i)
The result of runTestis not a list, therefore you can't give it to the forstatement. The reason that you get a bit of a strange error message, is due to how your foris desugared:
的结果runTest不是列表,因此您不能将其提供给for语句。您收到一些奇怪的错误消息的原因是您for的脱糖方式:
for (i <- powersList; j <- runTest(i)) yield i -> j
// turns into
powersList.flatMap { i => runTest(i).map { j => i -> j } }
However, the result of runTest(i)is a Double, which doesn't have a mapmethod. So looking at the desugaring, the error message actually makes sense.
但是, 的结果runTest(i)是 a Double,它没有map方法。所以看看脱糖,错误信息实际上是有道理的。
Note that my point about runTest's result not being a list is not really correct: Anything that has a mapmethod that will allow the above statement (i.e. taking some kind of lambda) will do. However, this is beyond the scope of this answer.
请注意,我关于runTest's result 不是列表的观点并不真正正确:任何具有map允许上述语句的方法的东西(即采用某种 lambda 表达式)都可以。但是,这超出了本答案的范围。
We now have successfully created a list of tuples (since powersListis a List, the result of the forloop is a Listas well). However, we want a Map. Luckily, you can call toMapon Lists that contain tuples to convert them into a Map:
我们现在已经成功创建了一个元组列表(因为powersList是 a List,for循环的结果也是 a List)。但是,我们想要一个Map. 幸运的是,你可以叫toMap上List包含元组,将它们转换成A S Map:
val tups = for (i <- powersList) yield i -> runTest(i)
timingsMap = tups.toMap
A note aside: If you really want to keep the jinside the for-loop, you can use the equals sign instead of the left arrow:
旁注:如果您真的想j将for-loop保留在内部,则可以使用等号而不是向左箭头:
for (i <- powersList; j = runTest(i)) yield i -> j
This will turn into:
这将变成:
powersList.map { i =>
val j = runTest(i)
i -> j
}
Which is what you want.
这就是你想要的。

