In this blog, we will be going to talk about partial functions. We know about functions in Scala. Let’s talk about it first.

So, what is a function?

A function is a mapping or a transformation of an input to an output.

For example,  we have function isEven, which takes an int value and retuns boolean value.

scala> def isEven(x :Int) = x % 2 == 0
isEven: (x: Int)Boolean

scala> isEven(12)
res0: Boolean = true

scala> isEven(121)
res1: Boolean = false

What if i pass a negative value or zero to it?

scala> isEven(0)
res2: Boolean = true

scala> isEven(-12)
res3: Boolean = true

It works fine. 0 and -12 both are an even values.  Let’s take another function square, which takes an int value and returns square value of input.

scala> def square(x: Int) = x * x
square: (x: Int)Int

scala> square(4)
res8: Int = 16

scala> square(12)
res9: Int = 144

scala> square(-2)
res10: Int = 4

Let’s take one more function square root. How do we define it ?

A square root is a function which takes an integer value greater than or equal to zero and gives an approximately half value of it. But what happens if an input value is negative? Then Square root is not defined because the input is invalid.

So, in other words, isEven and square both are function but the technically square root is not a function.  What is it then? Well, it is a partial function.  What does it mean by partial function?

 A Partial function is a mapping or transformation from the subset of input values to output.  There is some subset of values of input where the partial function is not defined.

For example, isEven and square is defined for all values of integer but square root is defined only for positive values of integer. It is not defined for negative values.

Let’s define square root function as regular function.

scala> def squareRoot(x: Int) = scala.math.sqrt(x)
squareRoot: (x: Int)Double

scala> squareRoot(16)
res0: Double = 4.0

What if we provide a value zero to it ?

scala> squareRoot(0)
res2: Double = 0.0

It works fine. But what if we try to give invalid input?

scala> squareRoot(-4)
res3: Double = NaN

It gives NaN, for all invalid inputs but this is not something good way to handle this. So, first thing we should avoide defining square root as a regular function. Define it as a partial function. Partial functions allows us to query it. It would be great if we can check whether the function is defined or not for particular value.

Let’s redefine again as a partial function,

scala> val squareRoot: PartialFunction[Int, Double] = {
| case x if x > 0 => scala.math.sqrt(x)
| }
squareRoot: PartialFunction[Int,Double] = <function1>

We have  defined squareRoot as a partial function which takes an input and returns double value. Now thought comes to mind we could do same pattern matching in a regular function definition. But there is a difference between function and partial function. Function can only be applied but it can not be queried. In case of partial functiions, we can query it whether the input value is defined or not.

scala> def callSquareRoot(x: Int) = {
| if(squareRoot.isDefinedAt(x)) squareRoot(x)
| }
callSquareRoot: (x: Int)AnyVal

scala> callSquareRoot(4)
res12: AnyVal = 2.0

scala> callSquareRoot(-4)
res13: AnyVal = ()

We have queried the partial function whether it is defined for -4 or not.  This time there is no NaN value.  Empty parenthesis is there because of else part.

So, we saw what is partial function and how it can be defined. We will explore more about it in future blogs. Till then stay tuned. 😀

Please feel free to suggest and comment.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.