First of all, why do we talk about currization

At the beginning, a friend showed me an interview question: Add (1)(2)(3)(4)=10; , the add (1) (1, 2, 3) and (2) = 9; When I saw this interview question, I resisted. Why write these parameters separately? Is this interviewer sand? Please remember the above sentence, the back will be crazy face. At that time I had heard of the currization of functions, but knew nothing about it.

Later, when I saw someone Posting this question on my favorite official account, I realized that the interview question was not so simple. I did not expect to be slapped so quickly.(Sorry, Miss Luo)

The above questions

Add (1) (2) (3) (4) = 10; , the add (1) (1, 2, 3) and (2) = 9;

#title

function add() { const _args = [...arguments]; function fn() { _args.push(... arguments); return fn; } fn.toString = function() { return _args.reduce((sum, cur) => sum + cur); } return fn; }Copy the code

For those of you who have not come into contact with Corrification, the above interview questions should be read for a while, and I will give you some ideas (I suggest you must write it down).

// Add does not have any arguments. // Add does not have any arguments. Return fn(); return fn(); return fn(); return fn(); return fn(); Return _args. Reduce ((acc, cur) => acc + cur) function add() {const _args = [...arguments] Function fn() {// 1, the function returns a function (can I declare a function first, then return) _args.push(... Arguments) // 3, return fn // 4, return fn inside function fn. } fn. ToString = function() {return _args.reduce((acc, Cur) => acc + cur)} return fn; Then return)} the console. The log (add (1) (2) (3) (4)) console. The log (add (1) (1, 2, 3) and (2))Copy the code

I’m glad I solved this interview question, and then what is Currification, and what does currification do? I still don’t understand

The Way of Learning by corrification

What is Corrification?

Changing a function that takes multiple arguments to a fixed form that takes one argument and returns one function, making it easier to call again

After reading the definition of Currification, I still do not understand, what are the application scenarios of Currification? So I lazily went to B station to watch a video spoken by a person and had an interview question, no more words, the topic

Const namelist1 = [{mid: 'profession:'}, {mid: 'profession:'}, {mid: 'profession:'}, {mid: 'profession:'}, }, {mid: 'clockwork ', profession:' clockwork '},] const namelist2 = [{adc: 'wheel mother ', profession:' adc '}, {ADC: 'VN', profession: 'ADC'}, {ADC: 'mouse ', profession: 'ADC' }, ] / / longhand. / / the console log (namelist1. The map (hero = > hero. Mid)) / / console log (namelist2. The map (hero = > hero. Adc)) / / writing / / curry const curry = name => element => element[name] // const mid = curry('mid') // const adc = curry('adc') // console.log(namelist1.map(mid)) // console.log(namelist2.map(adc))Copy the code

WTF what is this? Is it necessary? Why did I write a curry function? With this question in mind, I read many gold-digging blog posts, and finally there was an article that told me the function of Corrification

See clouds and fogs

Thanks to nuggets writer Bridge in the cloudsAfter reading the article, I feel that curry is really fragrantThere are several examples in this article, which I didn’t understand at the beginning. I didn’t know the function of Corrification until I saw this example

Currization actually complicates the simple problem, but it also gives us more freedom in the way we use functions. This freedom to handle function parameters is the core of Currization. The essence of currification is to reduce generality and improve applicability. Here’s an example:

At this time, we will encapsulate a universal function checkByRegExp to receive two parameters, the checked regular object and the string to be checked

function checkByRegExp(regExp,string) { return regExp.test(string); } checkByRegExp(/^1\d{10}$/, '18642838455'); / / check tel checkByRegExp (/ ^ (\ w) + (. \ \ w +) * @ (\ w) + ((. \ \ w +) +) $/, '[email protected]'); // Verify the mailboxCopy the code

The above code, which at first glance looks fine, satisfies all our requirements for passing the regular check. But let’s consider the question, what if we need to validate multiple phone numbers or email addresses?

We might do that

checkByRegExp(/^1\d{10}$/, '18642838455'); // Check phone number checkByRegExp(/^1\d{10}$/, '13109840560'); // Check phone number checkByRegExp(/^1\d{10}$/, '13204061212'); / / check tel checkByRegExp (/ ^ (\ w) + (. \ \ w +) * @ (\ w) + ((. \ \ w +) +) $/, '[email protected]'); / / check email checkByRegExp (/ ^ (\ w) + (. \ \ w +) * @ (\ w) + ((. \ \ w +) +) $/, '[email protected]'); / / check email checkByRegExp (/ ^ (\ w) + (. \ \ w +) * @ (\ w) + ((. \ \ w +) +) $/, '[email protected]'); // Verify the mailboxCopy the code

Every time we check all need to input a string of regular, check again the same types of data, the same regular we need to write many times, this leads to when we were in the use of inefficient, and because the checkByRegExp functions itself is a tool and doesn’t make any sense, after a period of time we revisit these code, Without annotations, we have to check the contents of the re to know if we’re checking a phone number, an email address, or something else.

At this point, we can use the kerrization to encapsulate the checkByRegExp function to simplify code writing and improve code readability.

Let _check = curry(checkByRegExp); // Generate a utility function to verify the phone number let checkCellPhone = _check(/^1\d{10}$/); / / generation tool function, validation email let checkEmail = _check (/ ^ (\ w) + (. \ \ w +) * @ (\ w) + ((. \ \ w +) +) $/); checkCellPhone('18642838455'); // Check the cellphone number ('13109840560'); // Check the cellphone number ('13204061212'); // Check phone number checkEmail('[email protected]'); // checkEmail('[email protected]'); // checkEmail('[email protected]'); // Verify the mailboxCopy the code

The above code is excerpted from the author of the Bridge in the Cloud at this time I understand the role of curryization, so let’s go back to the above station B that topic

Const namelist1 = [{mid: 'profession:'}, {mid: 'profession:'}, {mid: 'profession:'}, {mid: 'profession:'}, }, {mid: 'clockwork ', profession:' clockwork '},] const namelist2 = [{adc: 'wheel mother ', profession:' adc '}, {ADC: 'VN', profession: 'ADC'}, {ADC: 'mouse ', profession: 'ADC' }, ] / / longhand. / / the console log (namelist1. The map (hero = > hero. Mid)) / / console log (namelist2. The map (hero = > hero. Adc)) / / writing / / curry const curry = name => element => element[name] // const mid = curry('mid') // const adc = curry('adc') // Console. log(namelist1.map(mid)) // console.log(namelist2.map(adc)) That's a lot of trouble but let's just think about it, mid can be used multiple times and if you want an array with a property named mid, you can just say namelist1.map(mid) and if you want to think about it this way, if you want to make your code a little bit simpler by currizing it, And we also know that we're going to take the property of mid.Copy the code

But there are several more examples in the bridge in the Clouds article

1. The example of a

// Currization in mathematics and computational sciences: Function sum(a,b,c) {console.log(a+b+c)} function curry(fn) {//... Let _sum = curry(sum); // return A function that takes the second argument let A = _sum(1); // return A function that takes the third argument let B = A(2); // Receive the last argument, apply all previous arguments to the original function, and run B(3) // print: 6Copy the code

Let’s start by implementing the Curry function

  1. I started thinking about the add() method at the top of this article, but it didn’t work because it extracted the sum function, and I tried for a long time but couldn’t solve it
  2. So work backwards from the answer, up the answer
@param fn The number of arguments required by @param len, */ function curry(fn,len = fn.length) {return _curry.call(this,fn,len) */ function _curry(fn,len,... args) { return function (... params) { let _args = [...args,...params]; if(_args.length >= len){ return fn.apply(this,_args); }else{ return _curry.call(this,fn,len,... _args) } } }Copy the code

So let’s write the idea

  1. In general, collect parameters, return a function to receive the rest of the parameters, receive enough parameters, execute the original function, otherwise return curry to continue to collect parameters
  2. Retrieves the number of parameters by the length attribute of the function, when the received parameter is equal to the number of parameters
  3. Call the function fn and pass the collected arguments to fn as arguments
  4. Although the whole process is simple a few words, but this is to write thinking a whole day + code + look at the bridge in the cloud notes summary
  5. I suggest you do it several times by hand

2. Case 2

Function fn(a,b,c,d,e) {console.log(a,b,c,d,e)} _fn (1, 2, 3, 4, 5); / / print: 1, 2, 3, 4, 5 _fn (1) (2) the (three, four, five); / / print: 1, 2, 3, 4, 5 _fn (1, 2) (3, 4) (5); / / print: 1, 2, 3, 4, 5 _fn (1) (2) (3) (4) (5); / / print: 1, 2, 3, 4, 5Copy the code

The curry function above also works for this example

Three (3) examples

function checkByRegExp(regExp,string) { return regExp.test(string); } // let _check = curry(checkByRegExp); // Generate a utility function to verify the phone number let checkCellPhone = _check(/^1\d{10}$/); / / generation tool function, validation email let checkEmail = _check (/ ^ (\ w) + (. \ \ w +) * @ (\ w) + ((. \ \ w +) +) $/); checkCellPhone('18642838455'); // Check the cellphone number ('13109840560'); // Check the cellphone number ('13204061212'); // Check phone number checkEmail('[email protected]'); // checkEmail('[email protected]'); // checkEmail('[email protected]'); // Verify the mailboxCopy the code

The curry function above also works for this example

At this point, I know a little bit about function Currification, but there are placeholders to change the order of arguments passed in, which will be updated later…