# Currying in Javascript (tersely explained)

const add = (a,b) => a+b

const adder = a => b => a+b

const curry = fn => a => b => fn(a,b)// curry(add) is identical to adder

# TL;DR Summary

How are the above functions related?

`add`

is an ordinary binary function:`add(2,3)`

`adder`

is a higher-order function with unary inputs:`adder(2)(3)`

`curry`

transforms`add`

into`adder`

- enables the use of
**closures** - can be extended for an arbitrary number of inputs
- TOPICS:
*Higher-order functions, closures, partial application, currying, functional programming*

# Introduction

Currying is a somewhat advanced technique that has a simple precept — it converts a function that takes multiple arguments into a sequence of functions that each take a single argument.

Arithmetic functions, for example, are all `binary`

, that is they have 2 inputs (or parameters, or arguments). Functions with a single input are called `unary`

. **Curried functions are** **unary**. (Functions with more than 2 inputs/arguments are called variadic functions**.**)

And finally, in javascript a function is `invoked`

(or called) using parentheses: `foo()`

invokes the function `foo`

So for a simple addition function: take `(a,b)` and return `a+b`. Easy-peasy.

`const add = (a,b) => a+b`

add(2,3) // 7

# Higher-Order Functions

But we may want to encapsulate `a`

in a **closure**, and leave `b`

as the only input — say we want to add 5 to any given number — `add5(b)`

returns `b+5`

. Variable `a`

is "enclosed" in function `add1`

.

To do this, we create a **higher-order function (HOF)**. A **HOF** is a function that returns another function (or even multiple nested functions).

`const adder = a => b => a+b`

adder(5)(4) // 9

It might look a little weird to see the double invocation `adder(a)(b)`

, but that's by design. A regular function returns `fn -> result`

, but a **hof** returns `fn -> fn -> result`

.

So when using ‘adder’ directly, we have to make 2 function calls to get the final result.

And this isn’t limited to binary functions — there can be any number of inputs/nested functions: `fn --> fn ... -> fn --> result`

.`foo()()...()`

But since a closure is what we’re after in this example, we can create an intermediary function that encapsulates `a`

, so we're invoking the outer function with input `a`

and then returning the inner function, which expects the second input, `b`

. Here we'll create a function that adds 5 to any number `b`

.

`const add5 = adder(5)`

add5(4) // 9

# Currying

How do we transform `add()`

into `adder()()`

? You may have guessed the answer...!

const curry = fn => a => b => fn(a,b)// direct usage

curry(add)(5)(7) // 12// with closure

add5 = curry(add)(5)

add5(7) // 12

So where do we end up?

- We have an
`add`

function - We have an
`adder`

high-order function - Currying let’s us use
`add`

in the same way as`adder`

. In the example here, we created a simple closure

Say we want to do the same with a built-in function such as `Math.pow`

which takes 2 inputs `(number, exponent)`

. And, in this case we want to encapsulate the `exponent`

variable. Since that's the 2nd variable required, we can create a modified curry function:

const curryRev = fn => b => a => fn(a,b)

// a and b are declared in reverse orderconst pow5 = curryRev(Math.pow)(5)

pow5(2) // 32

So here, we’ve simply reversed the order of the inputs, so we can encapsulate the 2nd input, and leave the 1st one as a variable input.

# Partial Application

This is a bit redundant by this point, but throughout we have been using **partial application** of functions. A partial application is a function which has had some, but not yet all, of its arguments applied.

In the discussion of higher-order functions (HOFs) above, we used a partial application of the `adder`

function to create a closure for the first input with: `add1 = adder(1)`

Currying allows us to create partial applications of basic functions and treat them as HOFs. In this case we are able to treat `add`

as a HOF. But not all partial applications are the result of curried functions.

These techniques (HOFs) are an essential element in functional programmming; fuller discussion is beyond the scope of this introductory article.