2

Is there some simple way how to add new arguments to a partial function, so that resulting function is defined in the same domain as before (new arguments have no influence on its partiality)? Following code works, but seems a bit verbose.

  val func : PartialFunction[A, B] = ....

  val f = new PartialFunction[(A,C), B] {
    def isDefinedAt(x: (A,C)): Boolean = func.isDefinedAt(x._1)
    def apply(x: (A,C)):B = func(x._1)
  }
Suma
  • 33,181
  • 16
  • 123
  • 191
  • Could you please replace Editable, Unit and Unit with A, B and C. I think it can formulate your problem better. – Nami Feb 16 '15 at 19:52

1 Answers1

4

You can do this:

val f : PartialFunction[(A, C), B] = { case (a, _) if func.isDefinedAt(a) => func(a) }
Suma
  • 33,181
  • 16
  • 123
  • 191
Nami
  • 1,215
  • 11
  • 21