# Currying Existing Functions with JavaScript

July 31, 2022

This is the third post in a series on curried functions titled Curry Functions in JavaScript. This follows the previous post named Basic Curried Functions in JavaScript.

Basic curried functions are different from more advanced ones because basic currying doesn’t involve taking a function as an argument, whereas more advanced ones do. Basic curried functions are defined from scratch, whereas advanced currying involves using an already existing multary function.

From a higher level, currying is more of an approach of transforming an otherwise uncurried function to return a series of unary functions – regardless of whether it is hardcoded by the author or transformed at runtime.

We will consider several advanced cases differing in their complexity levels: first one involving a wrapper function defined manually from ground up, but basing it on the passed, to-be-curried function.

In the upcoming posts, we will also cover auto-currying, variadic currying, infinite currying and currying to a fixed arity.

### Currying Existing Functions Manually

In this example, we’ll use the `createMessage()` function we wrote in the previous post named Basic Curried Functions in JavaScript.

Let’s say, we have the function that returns the message string in hand:

``````function createMessage(greeting, name, message) {
return `\${greeting}, \${name}! \${message}`;
};
``````

Instead of writing a curried function from scratch for the above function, like this:

``````function createMessage(greeting) {
return function(name) {
return function(message) {
return `\${greeting}, \${name}! \${message}`;
};
};
};
``````

we can use the existing multary function by passing it to a wrapper function and then build the wrapper function on top of it:

``````function curryATernaryFunction(f) {
return function(greeting) {
return function(name) {
return function(message) {
return f(greeting, name, message);
};
};
};
};
``````

Then calling the wrapper function with `createMessage` will give us the curried function. We can use it for our purposes afterwards:

``````const curriedCreateMessage = curryATernaryFunction(createMessage);

``````

I named the above function `curryATernaryFunction` because I wanted to play with it a bit. We can actually curry other ternary functions (those that take three arguments) with it:

``````function addThreeThings(one, two, three) {
return one + two + three;
};