Back2Basics: Understanding Partially Applied Functions

Table of contents
Reading Time: 3 minutes

In this blog, we are going to talk about Partially applied functions and its use case. Before starting, first, we will emphasize that though Partial functions and Partially applied functions sound similar they are different from each other. To understand Partial Functions refer.

What is function application?

Applying a function is called calling a function. When we pass all arguments of the function, we can apply the function.   For example, we have a function product.

But there might be a scenario where the certain argument is repeated and we are calling function many times.  For instance, we always evaluate the doubled value. We can partially apply our function by passing 2 as an argument and skipping another parameter and passing the only type of the second parameter.

This is called Partially applied function of Product because the function is not fully applied yet. We can pass another argument to double apply it fully. The difference between function and partially applied function is that function returns a result but the partially applied function returns another function which again can be applied.

Here double is a function which takes an int value and returns an int value.

Now double returns result because now function is fully applied. It takes a 5 and multiplies it with 2. We can skip all arguments of a method using _.

Here res16 is a function value which takes two arguments and returns an integer value.

So, what we can do with them?

Partial Functions allows us to create specialized functions from other methods.

So, we can reuse methods by creating a library of specialized functions.  It helps to maintain the DRY principle.  We are not copying and pasting the code to make new functions. Another use of the partially applied function is to create curried functions.

Currying is a technique of creating multiple parameter groups. A function that takes multiple arguments can be translated into a series of function calls that each take a single argument. 

We have created a method product as multiple parameters group and treating it as partially applied function. It is a curried function because it returns a function which takes one argument and returns another function which again takes one argument and returns finally result.

The concept of currying and partially applied functions are closely related, but they are not exactly the same.

Every curried function is a partially applied function but vice versa is not true. 

I hope, you will find useful. Thanks for reading.

Feel free to suggest and comment.


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.