Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 19, 2019 12:44 am GMT

Functional programming - all you need to know about currying.

Currying is the process of taking a function that accepts n arguments and turning it into n functions that each accepts a single argument. The arity of a function is the number of arguments that it accepts. A function that accepts a single argument is unary, two arguments binary, three arguments ternary, and n arguments is n-ary. Therefore, we can define currying as the process of taking an n-ary function and turning it into n unary functions.

Currying is NOT named after curry the food; instead, it's named after Haskell Curry, a 20th century mathematician and logician. Haskell Brooks Curry also happens to have three programming languages named after him - Haskell, Brook, and Curry. The more you know.

Let's first look at a curried function:

const addThreeNumbers = a => b => c => a + b + c;const addStep1 = addThreeNumbers(1);const addStep2 = addStep1(2);const addStep3 = addStep2(3);console.log(addStep1); // b => c => a + b + cconsole.log(addStep2); // c => a + b + cconsole.log(addStep3); // 6

However, what if we wanted the best of both worlds?

const addThreeNumbers = (a, b, c) => a + b + c;const curriedAddThreeNumbers = ???;

In such a case, we would need a currying function! The following is a basic example:

const curry = func => {  // this is yet another closure!  const args = [];  const keepCurrying = arg => {    args.push(arg);    if (args.length === func.length) return func(...args);    return keepCurrying;  };  return keepCurrying;}

Now we can do:

const addThreeNumbers = (a, b, c) => a + b + c;const curriedAddThreeNumbers = curry(addThreeNumbers);const addStep1 = curriedAddThreeNumbers(1); // functionconst addStep2 = addStep1(2); // functionconst addStep3 = addStep2(3); // 6

Most times however, you can use a pre-made curry powder function in a library such as Lodash or Ramda:

const addThreeNumbers = (a, b, c) => a + b + c;const curriedAddThreeNumbers = _.curry(addThreeNumbers);curriedAddThreeNumbers(1)(2)(3); // 6curriedAddThreeNumbers(1, 2)(3); // 6curriedAddThreeNumbers(1, 2, 3); // 6

As you can tell, these functions are quite a bit more advanced. The above code doesn't even cover all the use cases - check out their respective docs for more information.

Now for the question you might really be wondering.

Why?

You might be thinking:

Currying seems like a total waste of time.

Why run a function three times when you can just run it once?

Who the heck cares about adding numbers incrementally?

And I would say that you're mostly correct. In 10 years of coding, I have never once needed to use a currying function, or encountered a memorable situation where the use of a currying function would have been beneficial.

However, the concept is incredibly important. It helps you to understand the nature of a JavaScript function. By exploring currying above, we have also looked at closures, nested functions, function transformers, and so much more - and that makes this concept extremely worthwhile.

It's very likely that you will not adopt a full functional programming approach when you actually write JavaScript code professionally. However, the principles of functional programming stand firm regardless of your exact methodology, and when used in moderation, they can prove extremely beneficial to making your code easy to understand.

Happy coding!

Further reading: Here's a dev.to article I wrote discussing the use of microservices in the frontend. Micro Frontends: a deep dive on the latest industry trend.

About me: I train students to become web developers at Better Coding Academy all across Australia. We help our students from no prior experience to full-time employment, and our graduates get starting salaries of $80-120k AUD. You can find us at codebetter.com.au.

I also upload videos onto YouTube about various web development topics. Subscribe to see new tech videos a couple times a week! https://www.youtube.com/c/BetterCodingAcademy


Original Link: https://dev.to/parkroolucas/functional-programming-all-you-need-to-know-about-currying-5k7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To