I'm trying to diff two arrays. Specifically, I have one array with some strings, and another with indices that I want to clean from the first array. At first I tried to use anonymous function annotation with "_", but it didn't work, and I have a hard time understanding why. Here is an example:
val a1 = Array("word1","word2","word3")
val ind = Array(1,2)
val a2 = a1.zip(a1.indices)
val not_working = a2.filterNot( ind.contains(_._2) )
> console>:15: error: missing parameter type for expanded function
> ((x$1) => x$1._2)
> a2.filterNot( ind.contains(_._2) )
> ^ <console>:15: error: type mismatch;
> found : Boolean required: ((String, Int)) => Boolean
> a2.filterNot( ind.contains(_._2) )
My assumption was that _ corresponds to, for example, ("word1",0) or ("word2",1) tuples. But I don't understand the error, why does it get Boolean type here? I though it would get the second part of the tuple.
Just for the sake of it I tried to rewrite it more explicitly, and it worked. Here is an example that does what I expect, and to my naive eye it looks like the only thing I changed is notation:
val working = a2.filterNot( x => ind.contains(x._2) )
I'm having hard time understanding the difference between two examples. Why does the second one work?