Currying in Typescript: A Short Introduction to Functional Programming

When talking about functional programming you’re probably thinking of pure functional programming languages such as F#. 

While pure functional languages is preferred, you can still write functional code in other languages such as Typescript, C#, Java and Python.

Since the world of functional programming is big and it’s difficult to cover everything in one tutorial, we’ll in this tutorial focus on the basic concept of currying using Typescript.

What Is Currying in Typescript

Currying is one of the fundamentals of functional programming. It is the process of transforming a function with multiple arguments into a chain of single-argument functions. 

If you’re unfamiliar or have never heard about functional programming, there’s an article which explains it perfectly. You can find the article here.

I get it that this concept may be hard to grasp based on the definition above, so let’s go through this concept with a basic practical example.

Consider a function which takes in multiple numbers and then returns the sum of the numbers:

const addNumbers(x: number, y: number, z: number): number {
  return x + y + z;
}

Here’s how you can call this function:

const sum = addNumbers(2,3,4) //sum = 8

If we rewrite this function as a curried function, this is how it would look like:

const addNumbers = (x: number) => {
  const innerAdd = (sum: number) => (y: number) => {
    const newSum = sum + y;
    return innerAdd(newSum);
  };

  return innerAdd(x);
};

And you can now chain as many calls as you want to the curried function, like this:

const sum = addNumbers(1)(2)(3)(4)(5)(1)(3) //sum = 19

A bit easier to understand the concept of currying when you can follow a practical example, am I right or am I right?

Why Use Currying

Now, when we’ve explained what currying is you may wonder why you would use it instead of just making one call to a multi-argument function.

There’s multiple benefits of currying compared to using regular multi-argument functions. Here’s some benefits of currying in Typescript:

  • The function becomes modular. You can now perform side tasks in between adding each number to the sum
  • When you have multiple currying functions, you can easily chain them together.

Conclusion

This short article explained the concept of currying. We guided you through an example on how to curry a function using Typescript. 

There’s a lot more advanced ways you can apply currying and when talking about functional programming, there’s even more concepts you can dig into.

This article serves as a short introduction to the world of functional programming using Typescript.


Hope you enjoyed reading this article. We’re looking forward serving you with more interesting articles and tutorials.

If you’re interested in learning more about web development and Angular in particular, you should check out our Angular articles here.

Happy Coding!

1 thought on “Currying in Typescript: A Short Introduction to Functional Programming”

  1. May I simply just say what a comfort to discover an individual who
    truly knows what they’re talking about on the net. You certainly understand how
    to bring an issue to light and make it important. A lot more people have
    to read this and understand this side of your story.
    I can’t believe you’re not more popular since you certainly possess the gift.

    Reply

Leave a Comment