Scala错误“价值图不是Double”的成员 [英] Scala error "value map is not a member of Double"

查看:157
本文介绍了Scala错误“价值图不是Double”的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:我接受了 gzm0的答案,因为它摇滚!



@Eduardo确实提出了一个建议:
(for(i< - 10..20; j = runTest(i))产生i - > j).toMap 这也让我运行build,他只是从来没有发过一个答案,@ gzm0的答案在概念上是AWESOME所以我接受了。



一旦我得到这个与不能调用构造函数有关的其他问题, 我将能够通过实际运行程序来测试这些LOL



问题:我在这个表达式中有错误,具体如何修复它,但更多通常我缺少FP或Scala来做出这个错误?

  timingsMap = for(i<  -  powersList; j< ;  -  runTest(i))产生i  - > j 

我正在为我的第一个Gradle / Scala项目编写算法分配分析。 Scala不是任务的一部分,所以我没有使用功课标签。除了我在Java中与Spark的工作,我是功能编程的全新功能,我确信这是问题。



这是一个代码片段,完整的.scala文件位于GitHub上,是的,或者如果我变得虚弱,我将发布完整的程序:)

  val powersList =列表(10到20)

//创建一个地图来跟踪两个功能,并产生定时
var timingsMap:映射[Integer,Double] = Map()

//调用runTest函数一次为每个我们想要的两个权力,从powersList,
//分配timingsMap的2值和对于该树大小的runTest的结果
timingsMap = for(i< - powersList; j< - runTest(i))产生i - > j

错误是:

  /home/jim/workspace/Scala/RedBlackTree4150/src/main/scala/Main.scala:36:价值地图不是Double 
的成员timingsMap = for(i< - powersList; j< - runTest(i))yield i - > j

认为我在做 timingsMap = ... line将 powersList 的所有元素映射到 i 对于循环的每次迭代,以及每次迭代映射到 j runTest(i)的返回值,以及然后将所有这些对并将它们放入 timingsMap 中。是否正在尝试在循环中使用 i 调用runTest(i)导致问题的方式?



runTest如下所示:

  def runTest(powerOfTwo:Range ):Double = {

//在这里创建树
var tree = new TreeMap [Int,Double]

//我们只关心创建一个树(x< - powerOfTwo)的值是
{
//将map中的下一个项目设置为key,随机数
tree + =(x - > math.random)
}

stopWatchInst.start()

//现在查看树中的所有值,
(x< - powerOfTwo){
//获取该值,不要花时间/开销将其存储在var中,因为我们不需要它
tree.get(x)
}

//停止观察检查时间报告和返回时间
stopWatchInst.stop()
val totalTime = stopWatchInst.elapsed(TimeUnit.MILLISECONDS)
loggerInst.info(运行2为+ powerOfTwo +的功率+ totalTime +ms)
返回totalTime
}
pre>




注意:我已经建议更改 j = 在此行中的 j < - :timingsMap = for(i< powersList; j< - runTest(i))产生i - > j



另一个建议不喜欢使用yield,建议替换(10到20).map ...



奇怪的部分是现有的代码在IntelliJ编辑器中不显示错误,只有当我运行它时才会中断。建议都会在IDE中给出类型不匹配错误。我真的想在概念上弄清楚我做错了什么,谢谢任何帮助! (当然我需要让它工作!)






尝试gzm0后,我下了同一条路...我提供的代码在我使用 gradle run ...之前不显示任何类型的不匹配,而当我提出建议的更改时,它会开始给我错误IDE ...但保持em来!这是基于gzm0s答案的最新错误:

  / home / jim / workspace / Scala / RedBlackTree4150 / src / main / scala / Main.scala:37:类型不匹配; 
found:List [(scala.collection.immutable.Range.Inclusive,Double)]
required:Map [Integer,Double]
timingsMap = for(i< - powersList)yield i - > runTest(i)


解决方案



  for(i<  -  powersList)
yield i - > runTest(i)

runTest 不是一个列表,因此您不能将它赋予语句。你得到一个奇怪的错误消息的原因是由于如何被排除:



<$ p (i< - powersList; j< - runTest(i))的$ p> 产生i - > j

//转成

powersList.flatMap {i => runTest(i).map {j =>我 - >但是, runTest(i)

c $ c>是一个 Double ,它没有地图方法。所以看看这个绝望,错误信息实际上是有意义的。



请注意,关于 runTest 的结果不是一个列表是不正确的:任何有一个映射方法将允许上述语句(即采取某种lambda)将做。但是,这超出了这个答案的范围。



我们现在已经成功创建了一个元组列表(因为 powersList 列表,循环的的结果是列表)。但是,我们需要一个 Map 。幸运的是,您可以在包含元组的列表中调用 toMap ,将其转换为映射

  val tups = for(i<  -  powersList)yield i  - > runTest(i)
timingsMap = tups.toMap

注释:如果你真的想要将 j 保存在 -loop之前,可以使用等号而不是左箭头:

  j 

这将变成:

  powersList.map {i => 
val j = runTest(i)
i - > j
}

你想要什么。


Explanation: I accepted gzm0's answer because it rocked!

@Eduardo did come in with a comment suggesting: (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.

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

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?

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.

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 :)

  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

What I think I am doing in that timingsMap = ... line is get all the elements of powersList mapped onto i for each iteration of the loop, and the return value for runTest(i) mapped onto j for each iteration, and then taking all those pairs and putting them into timingsMap. Is it the way I am trying to use i in the loop to call runTest(i) that causes the problem?

runTest looks like this:

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

Another suggestion didn't like using yield at all and suggested replacing with (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 conceptually what I am doing wrong, thanks for any help! (and of course I need to get it to work!)


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:

/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)

解决方案

You want:

for (i <- powersList)
  yield i -> runTest(i)

The result of runTest is not a list, therefore you can't give it to the for statement. The reason that you get a bit of a strange error message, is due to how your for is desugared:

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 map method. So looking at the desugaring, the error message actually makes sense.

Note that my point about runTest's result not being a list is not really correct: Anything that has a map method that will allow the above statement (i.e. taking some kind of lambda) will do. However, this is beyond the scope of this answer.

We now have successfully created a list of tuples (since powersList is a List, the result of the for loop is a List as well). However, we want a Map. Luckily, you can call toMap on Lists that contain tuples to convert them into a Map:

val tups = for (i <- powersList) yield i -> runTest(i)
timingsMap = tups.toMap

A note aside: If you really want to keep the j inside the for-loop, you can use the equals sign instead of the left arrow:

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.

这篇关于Scala错误“价值图不是Double”的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆