Currying is named after Haskell Curry, and it’s essentially just taking a function which takes multiple parameters, converting it in to a function which only takes one parameter, and returns a functions which then takes the next parameter.
function fetchWithAuth(token, endpoint) {
return fetch(endpoint, {
headers: {
Authorization: `Bearer ${token}`,
},
});
}
// Curried
const fetchWithAuth = (token) => (endpoint) =>
fetch(endpoint, {
headers: {
Authorization: `Bearer ${token}`,
},
});
// Reuse the partially applied function
const fetchAsUser = fetchWithAuth(userToken);
fetchAsUser("/api/posts");
fetchAsUser("/api/comments");
Currying lets you turn large functions into small, specific functions. Instead of passing the same arguments around repeatedly, you can lock them in once and reuse the result.
Basically, currying is just preloading a function with an argument to produce a new function that “remembers” it.
Here is a tiny helper that lets us use currying in other functions:
const curry = (fn) => {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args);
}
return (...nextArgs) => curried(...args, ...nextArgs);
};
};
Currying is just a clean way to write better functions by locking in arguments once instead of passing them around everywhere.