This structure:
{
case head :: Nil => head
case head :: tail => List(tail.size + 1, head)
}
is a syntactic sugar for a partial function, that's why compiler throws you this message, scalac thinks that you are trying to make a PartilFunction[List[A], List[A]]
. To fix this issue you have three ways:
1) Complete this to pattern matching construct:
pack(list) map { ls => ls match {
case head :: Nil => head
case head :: tail => List(tail.size + 1, head)
}
}
2) Use underscore syntax:
pack(list) map { _ match {
case head :: Nil => head
case head :: tail => List(tail.size + 1, head)
}
}
which is actually the same. Now, if you want, you can ascribe a type for ls, but scala can successfully infer it.
3) Use inline syntax:
pack(list) map {
case head :: Nil => head
case head :: tail => List(tail.size + 1, head)
}
It is possible because PartialFunction
is a subclass of Function
, so it is possible to use partial function directly instead of plain function.
And you have no point in the result type List[Any]
. 1) this type is not good, cause you loose all your type information and 2) you function is not tailrecursive, so you can drop it and let scalac infer it for you