# Currying Vs. Partially Applied Function

### Don't get these two functions confused.

Join the DZone community and get the full member experience.

Join For FreeIn this blog, I’m going to discuss the difference between currying and partially applied functions.

Currying splits methods with multiple parameters into a chain of functions — each with one parameter.

## Currying

First, let’s understand currying using an example:

`scala> def multiply(a: Int)(b: Int)(c: Int) = a * b * c `

This is is the same as:

`def multiply(a: Int) = (b: Int) => (c: Int) => a * b * c `

`Multiply`

is a curried function that takes three parameters: a, b, and c. Invoke the `multiply`

function as:

```
multiply(1)(_)
res9: Int => (Int => Int) = $$Lambda$1118/1682999176@30b1c5d5 1
```

This will result in another function that takes an integer as an input and yields a lambda expression (Int => Int).

```
scala> res9(2)(_)
res10: Int => Int = $$Lambda$1119/2012237082@27682fa9
```

This will result in another function that takes an integer as an input and returns an integer as result.

```
scala> res10(3)
res11: Int = 6
```

`Res11`

is the final result.

**Partially Applied Functions**

Pass to function less arguments than it has in its declaration. Scala returns a new function with rest of arguments that need to be passed.

```
def isInRange(leftBound: Int, num: Int, rightBound: Int): Boolean = {
if (leftBound < num && num < rightBound) true
else false
}
isInRange(_: Int, 5, _: Int)
will return another function that will take two integers as an argument and
result type will be of type Boolean (Int, Int) => Boolean.</p>
scala> isInRange(0, 8)
res0: Boolean = true
scala>(isInRange _).curried
res28: Int => (Int => (Int => Boolean)) = scala.Function3$$Lambda$1322/926382023@3e9cceff
```

This will split the `isInRange`

method into a chain of functions each with one parameter.

**Difference Between Currying and Partially Applied Functions**

**Partially Applied Function Example**

```
scala> def isDivisible(numberOne: Int, numberTwo: Int) = ((numberOne % numberTwo) == 0)
isDivisible: (numberOne: Int, numberTwo: Int)Boolean
scala> isDivisible _
res23: (Int, Int) => Boolean = $$Lambda$1285/1740173358@6fdc624
```

By partially applying a normal function ( `isDivisible`

), it results in a function (`res21`

) that takes all parameters [ (Int, Int) => Boolean ].

```
scala> res23(4)
res26: Int => Boolean = scala.Function2$$Lambda$1320/451962020@40c78bd1 3 4 scala> res26(2) 5 res27:Boolean = true
```

This will give us a true result.

**Currying Example**

```
scala> def isDivisibleCurried(numberOne: Int)(numberTwo: Int) = ((numberOne % numberTwo) == 0)
isDivisibleCurried: (numberOne: Int)(numberTwo: Int)Boolean
scala> isDivisibleCurried _
res22: Int => (Int => Boolean) = $$Lambda$1298/27138712@4ec8083
```

Partially applying a function on `isDivisibleCurried`

will create a chain of functions, one per parameter list [ Int => (Int => Boolean) ].

Thanks for reading!

Opinions expressed by DZone contributors are their own.

Comments