Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala multi-partition a map - type mismatch; Found (A,B) => Boolean required (A,B) => Boolean?

I'm trying to multi-partition a map based on a list of predicates.

I wrote the following function to do that:

def multipartition[A,B](map : Map[A,B], list : List[(A,B) => Boolean]) : List[Map[A,B]] = 
    list match {
        case Nil => 
            Nil
        case l :: ls => 
            val (a, b) = map partition l; // type mismatch; found (A,B) => Boolean, required: (A,B) => Boolean
            return a :: multipartition(b, ls)
}

The scala compiler (I'm running 2.9.1) fails at the indicated place with a "type mismatch; found (A,B) => Boolean, required: (A,B) => Boolean".

Has anyone ever seen anything like that? Any idea how to fix it?

Thanks,

LP

like image 631
LordPhoenix Avatar asked Sep 19 '11 10:09

LordPhoenix


2 Answers

partition expects Function[(A,B), Boolean], that is a function of one pair argument, not a function of two arguments (rather annoying that they are different)

So you need to write ((A,B)) => Boolean as the type of elements of your list

(The error message is not helpful at all, close to a minor bug)

like image 141
Didier Dupont Avatar answered Nov 03 '22 10:11

Didier Dupont


Complementing didierd's answer, you can solve it by writing it like this:

        val (a, b) = map partition l.tupled;
like image 26
Daniel C. Sobral Avatar answered Nov 03 '22 11:11

Daniel C. Sobral