6

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

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59

2 Answers2

11

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)

Didier Dupont
  • 29,398
  • 7
  • 71
  • 90
  • 2
    Don't know about 2.9.1, but on 2.9.0-1 the REPL gives the error message `found : (A, B) => Boolean required: ((A, B)) => Boolean` so that seems fine – Luigi Plinge Sep 19 '11 at 11:15
  • I had no way to test immediately. Hope there was no regression. Could it be that the REPL gives better messages than the compiler? – Didier Dupont Sep 19 '11 at 11:25
  • 4
    @didierd It seems there was a regression indeed. I opened [SI-5007](https://issues.scala-lang.org/browse/SI-5007) about it. – Daniel C. Sobral Sep 19 '11 at 11:45
7

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

        val (a, b) = map partition l.tupled;
Community
  • 1
  • 1
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681