xxxxxxxxxx
const createURL = baseURL => {
const protocol = "https";
// we now return a function, that accepts a 'path' as an argument
return path => {
return `${protocol}://${baseURL}/${path}`;
};
};
// we create a new functions with the baseURL value in it's closure scope
const createSiteURL = createURL("mysite.com");
const createCareersURL = createURL("mysite-careers.com");
// create URLs for our main site
const homeURL = createSiteURL("");
const loginURL = createSiteURL("login");
const productsURL = createSiteURL("products");
const contactURL = createSiteURL("contact-us");
// create URLs for our career site
const careersHomeURL = createCareersURL("");
const careersLoginURL = createCareersURL("login");
xxxxxxxxxx
a technique that applies a function
to its arguments one at a time, with
each application returning a new function
that accepts the next argument.
xxxxxxxxxx
/**
* The underlying base function is "add" which takes 3 arguments and return their sum.
*/
const add = (a, b, c) => a + b + c;
/**
* We need such a function which will transform the base function such that
* it can also process its argument one by one.
*/
const curry = (baseFunc) => {
// TODO: Do something with it.
};
const add3 = curry(add);
You might have noticed how the new curried sum function isn’t all too different from our very first sum function which was taking a single list of parameters with three arguments. So, what is the point of currying?
Our curried sum function is much more flexible than the non-curried version and gives us a more generic function which we can use in multiple different ways. For instance, we can simply pass the cube function to sum without passing the second parameter list (1,5). sum(cube) is valid on its own as it is a valid function call and returns another function. The second parameter of two integers can now be passed whenever required later on in the program.
Let’s wrap up our discussion in the next lesson by taking a closer look at how multiple parameter lists expand.