So this compiles:
def compress[T](list: List[T]): List[(T, Int)] =
{
list.zipWithIndex.filter { tuple => (tuple._2 == 0) || (tuple._1 != list(tuple._2 - 1)) }
}
This does not compile:
def compress[T](list: List[T]): List[(T, Int)] =
{
list.zipWithIndex.filter { (_._2 == 0) || (_._1 != list(_._2 - 1)) }
}
Why?
_does not meanx. Instead, it means “use the next parameter in the parameter list” or “convert this method into a function object”, depending on context. In your case, the second one is nonsense because you want a function of one variable but use_three times.Hint: use
xort. Spelling outtupleisn’t likely to help anyone, and the one-letter versions are as compact as_. Better yet,