I'm new at scala and trying to write function which returns map with all indices for each letter at given string. My code:
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
}
When I'm trying to compile I have an error:
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) {
It seems that something wrong with value of the loop. So I've added to the last line of loop 'true' and now everything works fine:
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
}
What's wrong in my code, and how can I fix it? Scala version: 2.12.6