Scala:类型参数不符合特征 Subtractable 的类型参数界限

标签 scala loops collections

我是 scala 新手,正在尝试编写函数,该函数返回给定字符串中每个字母的所有索引的映射。我的代码:

def group(string: String) = {
  val map = mutable.Map[Char, ListBuffer[Int]]()
  for (i <- string.indices) {
    val ch = string(i)
    if(map.contains(ch)) map(ch) += i
    else map += (ch -> ListBuffer(i))
  }
  map
}

当我尝试编译时出现错误:

Error:(14, 30) type arguments [?,Iterable[Any] with PartialFunction[Int with Char,Any] with scala.collection.generic.Subtractable[_ >: Int with Char <: AnyVal, Iterable[Any] with PartialFunction[Int with Char,Any] with scala.collection.generic.Subtractable[_ >: Int with Char <: AnyVal, Iterable[Any] with PartialFunction[Int with Char,Any] with scala.collection.generic.Subtractable[_ >: Int with Char <: AnyVal, Equals]]{def seq: Iterable[Any] with PartialFunction[Int with Char,Any]}]{def seq: Iterable[Any] with PartialFunction[Int with Char,Any]{def seq: Iterable[Any] with PartialFunction[Int with Char,Any]}}] do not conform to trait Subtractable's type parameter bounds [A,+Repr <: scala.collection.generic.Subtractable[A,Repr]] val v = for (i <- string.indices) {

循环的值似乎有问题。所以我在循环的最后一行添加了“true”,现在一切正常:

def group(string: String) = {
  val map = mutable.Map[Char, ListBuffer[Int]]()
  for (i <- string.indices) {
    val ch = string(i)
    if(map.contains(ch)) map(ch) += i
    else map += (ch -> ListBuffer(i))
    true
  }
  map
}

我的代码出了什么问题,该如何修复? Scala 版本:2.12.6

最佳答案

正如您在 https://docs.scala-lang.org/tutorials/FAQ/yield.html 中看到的那样

Scala’s “for comprehensions” are syntactic sugar for composition of multiple operations with foreach, map, flatMap, filter or withFilter. Scala actually translates a for-expression into calls to those methods, so any class providing them, or a subset of them, can be used with for comprehensions.

所以,你的for循环

for (i <- string.indices) {
  val ch = string(i)
  if(map.contains(ch)) map(ch) += i
  else map += (ch -> ListBuffer(i))
}

相当于

string.indices.foreach(i => {
  val ch = string(i)
  if(map.contains(ch)) map(ch) += i
  else map += (ch -> ListBuffer(i))
})

foreach方法接口(interface)为

def foreach[U](f: A => U): Unit

map(ch) += i 返回 ListBuffer,但 map += (ch -> ListBuffer(i)) 返回 map 。当编译器尝试识别 foreach 参数 f: Int => U 中的 U 时,它会得到 ListBuffer 之间的内容> 和 Map 并且不会编译它。

此外,如果您不在某处使用 if-else 表达式,编译器不会检查它的结果类型。

您可以通过像这样重写代码来修复代码

def group(string: String) = {
    val map = mutable.Map[Char, ListBuffer[Int]]()

    def update(i: Int): Unit = {
        val ch = string(i)
        if(map.contains(ch)) map(ch) += i
        else map += (ch -> ListBuffer(i))
    }

    for (i <- string.indices) update(i)
    map
}

但最好使用标准方法

def group(string: String) = string.toCharArray.zipWithIndex.groupBy(_._1).mapValues(_.map(_._2))

关于Scala:类型参数不符合特征 Subtractable 的类型参数界限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52255769/

相关文章:

javascript - 如何为循环的循环添加计数器编号?

javascript - foreach对象循环添加到堆栈顶部

collections - java Collections 常见方法面试题

scala - 如何使用 Scala 和 Play 2.5.x 在对象中获取 application.conf 变量?

c# - 如何在 C# 中的一个循环中重命名多个按钮

scala - Scala 中的 'yield' 是否等同于 map 函数?

c# - 查找集合中每个不同邮政编码最后一次出现的索引

java - Java 中比较器接口(interface)的构造函数

带有花括号的类的 Scala 实例化

Scala.Either getOrElse 方法