Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compose partial functions

I have two PartialFunctions f and g. They have no side effects and are quick to execute. What's the best way to compose them into another partial function h such that h.isDefinedAt(x) iff f.isDefinedAt(x) && g.isDefinedAt(f(x))?

It's also OK if h is a function returning an Option rather than a partial function.

I'm disappointed that f andThen g does not do what I want:

scala> val f = Map("a"->1, "b"->2)
f: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2)

scala> val g = Map(1->'c', 3->'d')
g: scala.collection.immutable.Map[Int,Char] = Map(1 -> c, 3 -> d)

scala> (f andThen g).isDefinedAt("b")
res3: Boolean = true

scala> (f andThen g).lift("b")
java.util.NoSuchElementException: key not found: 2
    at scala.collection.MapLike$class.default(MapLike.scala:228)
like image 411
tba Avatar asked Apr 12 '14 00:04

tba


People also ask

What is partial function example?

(definition) Definition: A function which is not defined for some inputs of the right type, that is, for some of a domain. For instance, division is a partial function since division by 0 is undefined (on the Reals).

What is a Scala partial function?

A partial function is a function that does not provide an answer for every possible input value it can be given. It provides an answer only for a subset of possible data, and defines the data it can handle. In Scala, a partial function can also be queried to determine if it can handle a particular value.

What is partial function in Python?

What is a Partial Function? Using partial functions is a component of metaprogramming in Python, a concept that refers to a programmer writing code that manipulates code. You can think of a partial function as an extension of another specified function.

What is function composition in Scala?

Function composition is a way in which a function is mixed with other functions. During the composition the one function holds the reference to another function in order to fulfill it's mission.


1 Answers

Here's a shorter way than the linked question, taken from this thread:

  val f = Map("a" -> 1, "b" -> 2)                 

  val g = Map(1 -> 'c', 3 -> 'd')                 

  def andThenPartial[A, B, C](pf1: PartialFunction[A, B], pf2: PartialFunction[B, C]): PartialFunction[A, C] = {
    Function.unlift(pf1.lift(_) flatMap pf2.lift)
  }                                               

  val h = andThenPartial(f, g)            //> h  : PartialFunction[String,Char]

  h.isDefinedAt("a")                      //> res2: Boolean = true
  h.isDefinedAt("b")                      //> res3: Boolean = false
  h.lift("a")                             //> res4: Option[Char] = Some(c)
  h.lift("b")                             //> res5: Option[Char] = None

This can also be wrapped up as an implicit class, of course:

  implicit class ComposePartial[A, B](pf: PartialFunction[A, B]) {
    def andThenPartial[C](that: PartialFunction[B, C]): PartialFunction[A, C] =
      Function.unlift(pf.lift(_) flatMap that.lift)
  }

  val h2 = f andThenPartial g         //> h2  : PartialFunction[String,Char]

  h2.isDefinedAt("a")                 //> res6: Boolean = true
  h2.isDefinedAt("b")                 //> res7: Boolean = false
  h2.lift("a")                        //> res8: Option[Char] = Some(c)
  h2.lift("b")                        //> res9: Option[Char] = None
like image 192
DNA Avatar answered Oct 08 '22 16:10

DNA