Today I learned how to make a very simple currying function in javascript. I was already familiar with the concept of currying, however after seeing this really simply currying function in javascript it really cemented the concept in my head.

Currying

Currying is a popular technique in functional programming. Many functional programming languages support currying natively. Here is a definition of currying directly from Wikipedia :

In mathematics and computer science, currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument. Put simply, it is a way to break down a function that takes multiple arguments into smaller functions that only take one argument.

Currying in javascript

In javascript currying is very simple to implement due to the closure feature. This is from Mozilla dev documentation on Closure :

… a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

Here is a very simple implementation of currying in javascript.

1
2
3
4
5
function curry (func, arg1) {
  return function(arg2) {
    return func(arg1, arg2)
  }
}

Here is an example of using the currying function.

1
2
3
4
5
6
function add(arg1, arg2) {
  return arg1 + arg2
}

const addThree = curry(add, 3)
console.log(addThree(7)) // This outputs 10

Benefits of currying

The above example is very simple, but there are many benefits to currying functions.

  • Composing a specialized version of a function so that you don’t have to continue to pass the same parameters and only pass the arguments that change or are important to the caller.
  • Composing higher order functions, or functions that take functions, makes code more declarative and easier to read.

Conclusion

Implementing a currying function in javascript is very helpful in understanding closures and currying. If you do decide to use currying in your javascript or typescript code base I would recommend first looking into using a functional library. The implementation of currying above is just for example purposes and is an incomplete implementation.