Today I'm going to be talking about currying and how it relates to Elixir. Yum, curry! Well, unfortunately we're not going to be talking about curry dishes or any kind of food, but the functional programming concept called "currying".

What is Currying?

For those of you who haven't encountered the concept before, currying is a method of partially applying functions so that the rest of the parameters can be applied later on.

That may not mean much to you, so here's some Javascript code that demonstrates the concept of currying.

//Define a multiply function using a made-up curry function,
//which creates a curried version of that function
const multiply = curry((x, y) => x * y);

//Create a double function by partially applying the multiply funciton
const double = multiply(2);

//Now we can fully call the original multiply function by calling 
//the double function with another parameters. The result is 6.
let result = double(3);

So currying is the ability to provide only a subset of the required parameters needed to call the function, and the result is another function that only requires the remaining parameters. In programming languages, curried functions will do this automatically without any additional code.

So if we wanted to call the curried multiply function in the above example directly, we would have to call it like this:

//Create the curried function
const multiply = curry((x, y) => x * y);

//The result is 6
let result = multiply(2)(3);

We have to call it this way because the curried version only takes a single parameter and returns another function. We then have to call that function with the second parameter. So the curried version of multiply is the equivalent of the following code, except that it's done automatically by the curry() function.

function multiply(x) {
	return function(y) {
		return x * y;
	}
}

Javascript does not offer any native currying functionality, but various libraries including the functional-oriented Rambda do offer currying functionality. Some functional languages, such as Haskell and ML, support currying natively. Amazingly, C# also has a currying library called Functify.

By the way, currying is named after the mathematician Haskell Curry, who did a lot of work on the concept. In case you are wondering, the Haskell programming language is indeed named after him as well. My first thought was that "Haskell Curry" sounds like a name two really geeky programmers would give their child, but those things are named after him, not the opposite way around.

Currying and Partial Application in Elixir

Elixir does not support currying natively. Many functional languages do, but Elixir is not one of them. There are libraries out there such as Currying that implement true currying for Elixir if you want it. It works pretty much like the Javascript examples from earlier.

So far I haven't felt a need for currying in Elixir, but I don't have a large amount of experience using curried functions. My experience involved some limited coding in Javascript with Ramda. Those of you with significant currying experience may want to check it out.

Now I didn't lead you all this way just to say that this is all useless in Elixir. Currying is one method of partial function application, but we can accomplish the same thing via another means.

We can use an anonymous function to provide partial function application. I've done similar things before, but this is the first time I'm calling it out as an effective method of partial function application Here's the same example from above in Elixir.

#Create a multiply function
iex> multiply = fn (x, y) -> x * y end
#Function<12.99386804/2 in :erl_eval.expr/5>
#Create the double function
iex> double = fn x -> multiply.(x, 2) end
#Function<6.99386804/1 in :erl_eval.expr/5>
#Call the double function
iex> double.(3)
6
#Create a function that doubles the value 3
iex> double_three = fn -> double.(3)
...> end
#Function<20.99386804/0 in :erl_eval.expr/5>
#Call the double_three function
iex> double_three.()
6

This isn't quite as slick or as automatic as currying, but it accomplishes the same thing. It's another tool to compose functions and use them as building blocks to create other functions, which can be used to create even more functions.

In fact, we could use this same technique in Javascript or C#, so it's hardly an Elixir-only concept.