Javascript Currying
What is the currying? How to create a curried functions? In this blog we are going to find answers for this questions.
Currying is advanced technique of working with functions. Actually this concept come from lambda calculus. It's used not only in JavaScript, but in other languages as well. So in this blog I'll explain in JavaScript.

Currying doesn't call a function. It just transforms it. Let's say you have f(a,b,c) function it takes many parameters. Curried function transform the function to pieces for example f function => f(a)(b)(c). So, to better understand lets make an example.
// Let's say you have a fetcher function to make a API call
function fetcher(url, options) {
return fetch(url, options);
}
// For each method you should pass a parameter to what kind of http method want to use
// This is not effective way to use a function when you learn currying
// Without curring
const data = fetcher('url.com', 'GET');
const data = fetcher('url.com', 'POST');
/*-------------------------------------*/
//
function curriedFetcher(options) {
return function (url) {
return fetch(url, options);
};
}
// Usage
curriedFetcher('GET')('url.com');
curriedFetcher('POST')('url.com');
curriedFetcher('POST', 'url.com'); // Don't Work!
// With arrow function
const curriedFetcherArrow = (options) => (url) => fetch(url, options);
const getRequest = curriedFetcherArrow('GET');
const postRequest = curriedFetcherArrow('POST');
// Usage
getRequest('url.com');
postRequest('url.com');As you can see, the implementation is quiete easy. Also in line 19 something you might be use it case of some how. But it doesn't work like that. You might use a library for it. The common utils library lodash also has a curry function to transform functions to curried functions. And also you can work with previous version of function. That will be good if you need it. Let's checkout how can implement a library for it.
Conclusion
In this blog we learned what is currying and how to implement it. Also we learned how to use it with lodash. I hope you enjoyed it. If you have any questions or suggestions please reach me with links on top of the page. Thanks for reading.