Javascript: Understanding Function Currying.

December 19, 2017

Understanding Function Currying in JavaScript: Simply Explained.

In the dynamic world of Javascript development, mastering various programming paradigms and techniques can significantly enhance your coding skills and project outcomes. One of these techniques is currying, a functional programming concept that can help you create more flexible and dynamic functions.

What is Currying?

Simply put, it is a technique where a function with multiple arguments is transformed into a group of nested functions, each taking a single argument.

In other words, it is a function, that returns a function, that returns a function, and so on, until the final function returns the result of the computation.

Example of Currying

function curryingFunction(arg1) {
  return function (arg2) {
    return function (arg3) {
      return `Final result with ${arg1}, ${arg2}, and ${arg3}`;
    };
  };
}

curryingFunction(arg1)(arg2)(arg3);

Example of Regular Function

function regulatFunction(arg1, arg2, arg3) {
  return `Final result with ${arg1}, ${arg2}, and ${arg3}`;
}

curryingFunction(arg1, arg2, arg3);

Why Use Currying?

At first glance, it might seem more straightforward to handle all arguments in one go, especially since the order in which arguments are passed remains important. However, currying offers the flexibility to pass arguments as they become available.

This is particularly useful in scenarios where you only have a subset of necessary information initially and obtain the rest later.

Consider this example, where we incrementally receive details to form a response:

function prepareResponse(activity) {
  return function (tool) {
    return function (person) {
      return `${activity} performed with the ${tool}, led by ${person}.`;
    };
  };
}

let activitySetup = prepareResponse("Programming");

// Program goes on.

let toolSetup = activitySetup("Mac");

// More details received later

let finalMessage = toolSetup("Billy");

console.log(finalMessage);

// Output: Programming performed with the a Mac, led by Billy.

Currying as Partial Application

Currying is closely related to the concept of partial application ā€” where a function with multiple parameters is partially applied to create a new function.

JavaScript allows this flexibility because functions are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments, or returned from other functions.

function sendMessage(message) {
  return function (recipient) {
    return `Message to ${recipient}: ${message}`;
  };
}

let welcomeMessage = sendMessage("Welcome to the platform!");

console.log(welcomeMessage("Billy"));

// Output: Message to Billy: Welcome to the platform!

Conclusion

Currying is a foundational concept in functional programming, borrowed from languages like Haskell and Scala.

Understanding and utilizing currying in JavaScript can provide a new perspective on handling functions and parameters, allowing you to structure your programs more dynamically and flexibly.

Got questions?

Or want to discuss these concepts further?

Iā€™d love to hear from you! Reach out via Linkedin or š•.