Back2Basics: Understanding Partial Function – #2

In previous blog Understanding Partial Functions – #1, we have talked about what is partial function and how they are different from regular functions. In this blog, we will explore more about the partial function.

We have talked about how we can define partial functions having one argument. But what to do if we have to pass more than one argument.

Let’s try defining divide method which takes two arguments, as a partial function,

Compiler blows up. So how to receive more than one arguments? Since the partial function of the type, PartialFunction[A, B] where A is the return type of argument and B is the return type of returned value from partial function. So, we could treat more than one argument as a tuple.

It worked. So we can pass more than one arguments as a tuple to a partial function.

Suppose we have a problem that first evaluates the division of two numbers and then calculate the square root of them. It would be a bigger partial function.  We can use lots of utility methods to create bigger partial functions using smaller partial functions. Let’s see  first create square root as a partial function,

We can perform required calculation like:

This works but it neither looks expressive nor it is composable. We have methods andThen and compose which can help us composing bigger partial functions using smaller partial functions.

Let’s give it a try,

It looks more elegant.  We can make code more expressive, concise and composable using andThen and compose.

What is the difference between them?

andThenIt first evaluates left and then the right partial function. Second, it returns partial function.

composeIt first evaluates right and then left partial function. Second, it returns a function instead of partial function.

Now we will talk about other functions we can use with partial functions i.e applyOrElse, lift functions.

We have seen that when we provide invalid input to square root function, it gives us NaN. So properly handling it, we first query it and then apply the function if input is valid. We can have alternate way to do same thing using applyOrElse instead of apply.

applyOrElse: We are saying if input is in domain, apply it else apply the alternate method. So applyOrElse takes an input and function for else part. Here we are going to return zero for all invalid inputs.

There is another function lift which lifts up partial function to a regular function. Partial function is applied when value is in domain otherwise it blows up with match error.  We can also do if it is defined give me result otherwise none.

We can do that first checking whether input is defined or not. If it is defined, return value wrapped in Some otherwise None. But we don’t need to write that much code. Instead we can use lift.

So, we saw how we could pass more than one arguments to partial functions. How we could create bigger partial functions from smaller partial functions using andThen and compose. We explored about applyOrElse and lift methods. I hope you enjoyed it while reading.

Feel free to suggest or 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.

1 thought on “Back2Basics: Understanding Partial Function – #2

Leave a Reply

%d bloggers like this: