## Currying and closures, functional programming only?

Currying and closures really shine in ML or Lisp style functional languages like Scala, Haskell, Scheme or Clojure where they allow to encapsulate computation elegantly. However both concepts are not implemented uniquely in functional languages: as ideas from different backgrounds converge together, these days is possible to have both in languages like Python, that despite being OO also partially implements the functional paradigm.

Before we kick off with some code a bit of background: currying is named after the mathematician Haskell Curry that that developed techniques to partially apply lambda calculus functions.

Speaking of functions in mathematics a function is a relationship or a mapping between a set of input values and a set of possible outputs. You can follow this [link] (https://en.wikipedia.org/wiki/Function_%28mathematics%29) for a lengthy explanation about mathematical functions, but for now suffice to say that a function can be thought as the specification of how to manipulate an input to produce the output (please mathematicians have mercy). Another important aspect of mathematical functions is that their specification is fixed, that is they will always produce the same output given the same input.

Many programming languages also provide a construct called function, although in most of those languages functions do not quite resemble their mathematical counterpart. For this reason is perhaps easier to think of them as a technique to enclose a bit of computation in a box so it can be accessed easily. In mathematics a function is just the definition of the operation, so each time we want to run the function we *must* supply all the input values. However sometimes in programming languages it is convenient to have a function access some of the values in the background, so that we can avoid supplying all of them each time. This behaviour is obtained with function currying, or partial application.

Currying can only be used in languages that feature first-class functions, that is functions that return functions, or accept functions as parameters. Currying is rarely seen in pure OO languages like Java (at least up to 7), mainly because is really not a preferred technique in that programming paradigm. However some languages that partially implement functional programming like Python allow to do it, and a programmer might find it useful. Here is an example implementation in Python.

` ````
def curry_pair(fun, first): return lambda second: fun(first, second)
```

The one above is the simplest implementation of a currying function, that is a function that takes another function and its first argument and returns another function ready to take the second argument and pass it to the original function. I find it amazing how 68 characters can pack so many concepts. In action the currying function could be used like so:

```
>>> def add(x, y): return x+y
>>> add_2 = curry_pair(add, 2)
>>> add_2(2)
4
```

The code shows that `curry_pair`

has to both accept and return functions in order to work, which explains why only languages that implement first-class functions can allow currying.

Another concept that goes hand in hand with currying shown by `curry_pair`

is that of the closure. The closure is a private namespace "closed" around the curried function and is what allows the curried function to access the already supplied values. Perhaps most importantly (apart from rare exceptions) closures guarantee that the values already supplied cannot be changed but only accessed. This is sometimes called referential transparency. Referential transparency is what makes closed functions "mathematical", that is given a certain input will *always* return the same output.

Finally as the name implies `curry_pair`

can only work on functions that accept a pair of arguments. A Python workaround to make the currying function deal with functions that accept an arbitrary number of parameters is:

```
def curry_2(fun, first):
def wrapper(*args, **kwargs):
return fun(first, *args, **kwargs)
return wrapper
>>> def add_3(x, y, z): return x+y+z
>>> add_triplet = curry_pair(add_3, 2)
>>> add_triplet(2, 2)
6
```

Which brings us further down in the rabbit hole. You probably have already noticed that the definition of `curry_2`

looks suspiciously similar to that of a decorator. That is exactly the case, as a decorator can act as a "compile-time" currying function used to create a closure oven another function. This is a good example of how ideas from maths and functional programming can trickle down to more industry-like paradigms like OO.

This is however as far as it is wise to draw the parallel, because decorators are not just currying functions as they can be used on classes or even modules. Also decorators create closures only when first loaded, not at any point in the run-time.

Now that we've seen currying in OO I'd like to show an example of Haskell, a language that implements curried functions out of the box. For example foldr (reduce in Python) takes 3 arguments: a function taking 2 arguments, an initial value and a sequence of values. Let's assume we want to use a fold to calculate the sum of a list of integers, we could easily define it as:

```
Prelude> let sum = foldr (+) 0 :: [Int] -> Int
Prelude> sum [1,2,3,4,5]
10
```

Now the `sum`

function is ready to accept *any* list of integers. What if we want to calculate the factorial of a list of Int?

```
Prelude> let factorial = foldr (*) 1 :: [Int] -> Int
Prelude> factorial [1,2,3,4,5]
120
```

And isn't that just beautiful? Those two brief examples show how powerful fully implemented currying can be in allowing a programmer to re-use simple, general building blocks to create more specific ones.