Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore in List.filter

Tags:

scala

Why this is not working:

List(true,false).filter(_).size

The error says:

<console>:8: error: missing parameter type for expanded function 
((x$1) => List(true, false).filter(x$1).size)
    List(true,false).filter(_).size
                            ^

but the following works (looks just the same for me):

List(true,false).filter(a => a).size

I'm using Scala 2.9.0.1.

like image 784
Rogach Avatar asked Oct 08 '11 06:10

Rogach


People also ask

How to use filter in Underscore js?

The _. filter() is used to check which elements in the passed array satisfy the condition. It will form a new array of all those elements which satisfy the condition passed from the array. It is mostly used when need to find certain elements from a large array.

What is the function of Underscore?

The underscore is used as a diacritic mark, "combining low line", ◌̲ , in some languages of Egypt, some languages using the Rapidolangue orthography in Gabon, Izere in Nigeria, and indigenous languages of the Americas such as Shoshoni and Kiowa.


2 Answers

Handling of _ is a bit tricky in Scala and as a side note I think error handling should be improved a bit. Back to topic, have a look at this example:

def twice(i: Int) = i * 2
def gt2(j: Int) = j > 2

List(1,2,3) filter gt2

This compiles fine and works as expected. However trying to compose functions results with cryptic error:

List(1,2,3) filter gt2(twice(_))

  error: missing parameter type for expanded function ((x$1) => twice(x$1))
          List(1,2,3) filter gt2(twice(_))
                                     ^ 

What happened? Basically when Scala compiler sees underscore it binds it into the most immediate context, which is twice(_) in this case. This means we are now calling gt2() with a function twice as an argument. What compiler knows is that this function takes an argument and returns the same type. Arguably it should figure out the type of this argument and return type is Int based on twice() signature, however it uses x$1 placeholder temporarily until he figures that out later.

Unfortunately it is unable to do that since gt2() expects an Int while we are providing a function (at least that is what the compiler thinks).

So why does:

List(1,2,3) filter {k => gt2(twice(k))}

work? The compiler does not know the type of k in advance. However it knows that gt2 returns Boolean and expected an Int. It also knows that twice() expects an Int and returns one as well. This way it infers the type of k. On the other hand the compiler knows from the beginning that filter expects Int => Boolean.


That being said back to your case. The underscore alone ((_)) is not consider a "context" so the compiler searches for another most immediate enclosing context. The !_ would have been considered a function on its own, as well as _ == true. But not the underscore alone.

So what is the closest immediate context (I'm sure there is a scientific name for that...) in this case? Well, the whole expression:

(x$1) => List(true, false).filter(x$1).size

The compiler thinks you are trying to create a function that takes some parameter of unknown type and returns something of the type of an expression: List(true, false).filter(x$1).size. Again arguably it should be able to figure out that filter takes Boolean => Boolean and returns Int (.size), but apparently it doesn't.


So what can you do? You have to give the compiler a hint that the underscore should be interpreted in smaller context. You can say:

List(true,false) filter (_ == true)
List(true,false) filter (i => i)
List(true,false) filter identity
like image 135
Tomasz Nurkiewicz Avatar answered Nov 06 '22 09:11

Tomasz Nurkiewicz


The first error is because Scala doesn't know what to make of _. So try this...

List(true,false).filter(_:Boolean).size

After that, you get more info:

<console>:8: error: type mismatch;
found   : Boolean
required: (Boolean) => Boolean
 List(true,false).filter(_:Boolean).size

It's just evaluating the _ just as the value and not as function. Per the ScalaDoc

filter (pred: (A) ⇒ Boolean): GenTraversable[A] 
like image 24
Daniel Hinojosa Avatar answered Nov 06 '22 09:11

Daniel Hinojosa