Back2Basics: Understanding Partial Functions – #1

monads

 

In this blog, we will 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.

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

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.

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 a value when it is multiplied by itself produces aproximately equal to same number. 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.

What if we provide a value zero to it ?

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

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,

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.

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.


knoldus-advt-sticker


 

Written by 

I am a Software Consultant with experience of more than 1.5 years. I am a Functional Programing i.e Scala and Big Data technology enthusiast.I am a active blogger, love to travel, explore and a foodie.

6 thoughts on “Back2Basics: Understanding Partial Functions – #1

Leave a Reply

%d bloggers like this: