Of course, this article is not a recipe. It is used as a cooking tool and can only stay at the stage of eating curry. Curry, an English Writing of Curry, has another meaning. It is an experimental function programming language based on Haskell, mixed with function and logic programming... syntaxHighlighter. all (); of course, this article is not a recipe for cooking. Currently, it can only stay at the stage of eating curry. Curry, an English Writing of Curry, has another meaning. It is an experimental function programming language based on Haskell, mixed with function and logic programming, and added the Constraint Programming feature. It is named by the mathematician Haskell Curry. I have to say that this mathematician has a great influence on computers. However: this is not a great mathematician or Haskell language. Next I will describe a scenario. Javascript, as a functional programming language, is called an application. Developers with some JS programming experience can quickly think of built-in functions. prototype. apply () method. When applying a function, we can write: 1 var sayHello = function (param) {2 return 'hello, '+ param; 3} 4 5 sayHello. apply (null, ['Tom ']); // this inside 'hello, Tom' sayHello () points to a global object. Alternatively, let JS complete mathematical operations for us: 1 function add (x, y) {2 return x + y; 3} 4 5 add. apply (null, [1, 2]); // 3 when applying a function, all the parameters are passed in at one time. However, in actual application, we may encounter a situation where we do not need to input all parameters at a time. If the write method of the add () function remains the same, an error will occur when only one parameter is input, this is not what we want to see. What do we want to see? Here we will first YY: 1 var add = function (x, y) {2 return x + y; 3} 4 5 // some applications 6 var partialAdd = add. partialApply (null, [1]); 7 partialAdd. apply (null, [2]); // step 3 provides us with a new function that can be called. Then we can use other parameters to call this new function. Does it look pretty? Unfortunately, there are no partialApply () methods and functions in Javascript. However, front-end developers are all enterprising and strong. In addition, JS is a language with high flexibility. We can construct such application functions to meet our needs. Is it related to the curry mentioned earlier? Some, this is a process in which functions can understand and process some application scenarios. We call it the Curry process (also called the function's kernelization ). (Don't spray me, It's hard to write an article that is interesting to read. No one will read anything that is eye-catching.) back to what I said before, for a short addition operation, for example, 1 + 2, some application implementation ideas can be written as add (1) (2), which requires that we do not produce errors after calling add (1, return a function that can pass in the second parameter. The answer is: 1 function add (x, y) {2 // Part 3 if (typeof y = 'undefined') {4 return function (y) {5 return x + y; 6} 7} 8 9 // completely 10 return x + y; 11} is actually a common function transformation technique, called the incomplete function call (partial application ). This function transformation feature returns a parameter for each call until the final running result is obtained. But can we use a more general method to process the same task? The answer is naturally yes: function curry (fn) {var slice = Array. prototype. slice, stored_args = slice. call (arguments, 1); return function () {var new_args = slice. call (arguments), args = stored_args.concat (new_args); return fn. apply (null, args) ;}} in this way, the input parameters are saved in the returned function, and saved in variable a. The first parameter is stripped at the beginning of the returned function, because this parameter is a function that is about to be curry, and a private reference pointing to the slice () method is also saved. When we access the returned function, the new function merges some of the original application parameters into the new parameters, and then applies the merged parameters to the original function. In this way, you can use the previously defined add () function to test the general method of making any function curry. // Normal function add (x, y) {return x + y;} // curry a function to obtain a new function var newadd = curry (add, 1); newadd (2); // another method, curry (add, 3) (4); // continuous curry var newadd = curry (add, 1 ); var anothernewadd = curry (newadd, 2); in fact, there are many improvements to curry, for example, how to compile a suitable curry function based on the actual situation when there are requirements on the parameter type and sequence. I will add more content in the future. You are also welcome to speak freely and give more comments.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service