# The Difference Between Currying and Partially Applied Functions

### Currying and partially applying may seem the same — both turn your functions into ones with fewer arguments. But there's a difference between them.

Join the DZone community and get the full member experience.

Join For Freein this article, i want to show a real difference between curried functions and partially applied functions in scala. this question is pretty common for those developers who started learning scala without previous experience in functional programming. moreover, this blog post may be useful even for experienced scala developers because, based on my experience, i have had an incorrect understanding of the difference between the curried functions and partially applied functions.

so here is the most classic example of misunderstanding in the question of currying and partially applied functions. i wrote that article ~9 months ago. since then, i've dived deeper into scala and figured out what a curried function is and what a partially applied function is.

## definitions

before demonstration of code examples, it would be rational to get acquainted with the definitions. for most people, it is enough in order to understand what is the difference.

**
currying
**
: decomposition of functions with multiple arguments into a chain of single-argument functions.

notice, that scala allows passing a function as an argument to another function.

**
partial application of function
**
: pass to a function fewer arguments than it has in its declaration. scala does not throw an exception when you provide fewer arguments to the function, it simply applies them and returns a new function with rest of arguments that need to be passed.

in general, you can stop reading this article if everything is clear. otherwise, i recommend looking at some examples for each of function types.

## currying examples

let’s start from the most trivial example of a curried function:

```
def sum(a: int, b: int): int = a + b
//(int, int) => int
def curriedsum = (sum _).curried
//int => (int => int)
curriedsum(5)
//int => int
res0(10)
//int = 15
```

what happened in the code snippet above? at first, i declared the function
```
sum
```

. it has two arguments of the
```
int
```

type. its returning type is
```
int
```

as well.

then, i create a curried version of the
```
sum
```

function. the
```
curriedsum
```

function accepts a single argument of the
```
int
```

type and returns a function of the
```
int => int
```

type.

when i pass 5 into the
```
curriedsum
```

, i receive back a new function
```
res0: int => int
```

(this sample was performed in repl).

finally, invoking
```
res0
```

with any
```
int
```

parameter we will add this parameter to 5.

by invoking a
```
curried
```

method on any function of type
```
functionn
```

where n > 1, we receive a curried version of the function.

here is another example of currying:

```
def isinrange(left: int, n: int, right: int): boolean = {
if (left < n && n < right) true else false
}
(isinrange _).curried
//int => (int => (int => boolean))
```

the logic of the
```
isinrange
```

function is pretty simple: if argument
```
n
```

is more than
```
left
```

but less than
```
right
```

, the function returns
```
true
```

. otherwise, it returns
```
false
```

.

after we made a currying version of
```
isinrange
```

, we can perform the following operations with it:

in this screenshot, you can see how we pass arguments one by one and, as a result, we see two boolean results in
```
res3
```

and
```
res4
```

.

## partial application examples

now let’s see how partially applied functions look likein practice. i’m going to use the
```
isinrange
```

function from the previous section.

```
def isinrange(left: int, n: int, right: int): boolean = {
if (left < n && n < right) true else false
}
def is5inrange = isinrange(_: int, 5, _: int)
//(int, int) => boolean
is5inrange(0, 8)
//true
def between0and10 = isinrange(0, _: int, 10)
//int => boolean
between0and10(5)
//true
between0and10(100)
//false
```

so what happened in the code snippet above? i used
```
isinrange
```

as a base for two new functions:
```
is5inrange
```

and
```
between0and10
```

. notice, that for missed parameters, you can use an underscore.

partial application is extremely helpful when you want to create a set of different functions on top of one particular function. in the same way, as i create
```
between0and10
```

, it’s easy to create
```
between50and75
```

, for instance.

## summary

so after the theory and practice part, i hope you understood how curried functions differ from partially applied functions. once again: currying – transformation of a function with multiple arguments into a chain of single-argument functions. partially applied function – a function that was called with a fewer number of arguments.

Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments