# Back2Basics: Understanding Partially Applied Functions

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.

```scala> def product(x: Int, y: Int) = x * y
product: (x: Int, y: Int)Int

scala> product(2,3)
res1: Int = 6```

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.

```scala> val double = product(2, _: Int)
double: Int => Int = \$\$Lambda\$1141/1562251195@2eed37f4```

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.

```scala> double(5)
res3: Int = 10```

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 _.

```
scala> product _
res16: (Int, Int) => Int = \$\$Lambda\$1271/2134251218@7e5c04a4```

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.

```
scala> val double = product(2, _: Int)
double: Int => Int = \$\$Lambda\$1144/95553286@16aed49f

scala> val triple = product(3, _: Int)
triple: Int => Int = \$\$Lambda\$1145/812229472@7235f92b

scala> val quadruple = product(4, _: Int)
quadruple: Int => Int = \$\$Lambda\$1146/1523683407@a146b11

scala> triple(4)
res4: Int = 12
res5: Int = 16```

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.

```
scala> def product(x: Int)( y: Int) = x * y
product: (x: Int)(y: Int)Int

scala> val curriedFunction = product _
curriedFunction: Int => (Int => Int) = \$\$Lambda\$1269/2114223493@3d306fd5

scala> res17(4)
res18: Int = 8```

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 Mahesh Chand

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.