Value *不是AnyVal的成员

时间:2014-08-03 18:44:17

标签: scala scala-collections

这是我写的折叠,我收到了这个错误:

Error:(26, 42) value * is not a member of AnyVal
        (candE.intersect(candR), massE * massR)

                           ^

allAssignmentsTableList[Map[Set[Candidate[A]],Double]]

val allAssignmentsTable = hypothesis.map(h => {
    allAssignments.map(copySet => {
      if(h.getAssignment.keySet.contains(copySet))
        (copySet -> h.getAssignment(copySet))
      else
        (copySet -> 0.0)
    }).toMap
  })

val aggregated  = allAssignmentsTable.foldLeft(initialFold) { (res,element) => 
  val allIntersects = element.map { 
    case (candE, massE) => 
      res.map { 
        case (candR, massR) => candE.intersect(candR), massE * massR
      }.toList
  }.toList.flatten

  val normalizer = allIntersects.groupBy(_._1).filter(_._1.size == 0).map { 
    case(key, value) => value.foldLeft(0.0)((e,i) => i._2 + e)
  }.head

  allIntersects.groupBy(_._1).map { 
    case(key, value) => key -> value.foldLeft(0.0)((e,i) => i._2 + e)
  }
}

如果我这样做:case(candE, massE:Double)那么我不会收到错误,但我会在匹配中获得异常。

1 个答案:

答案 0 :(得分:2)

你遇到的问题:

val aggregated  = allAssignmentsTable.foldLeft(initialFold) { (res,element) => 
  val allIntersects = element.map { 
    case (candE, massE) => 
      res.map { 
        case (candR, massR) => candE.intersect(candR), massE * massR
      }.toList
  }.toList.flatten

最有可能来自前一个代码块:

val allAssignmentsTable = hypothesis.map(h => {
    allAssignments.map(copySet => {
      if(h.getAssignment.keySet.contains(copySet))
        (copySet -> h.getAssignment(copySet))
      else
        (copySet -> 0.0)
    }).toMap
  })

我的假设是h.getAssignment(copySet)返回别的东西而不是Double(这似乎是由OP中引用的错误消息 - (26, 42)等确认的),这两个值都看起来不像是因此,allAssignmentsTable undercover可能不是List[Map[Set[Candidate[A]],Double]],而是其他东西,例如它有Any而不是Double,因此无法应用operator *