This article turns from: 1190000003733107
The function of Curry refers to the process of evaluating the parameters gradually.
I think it is: reduce commonality, improve the specificity.
Typically, the process of Gerty is that "if you fix certain parameters, you will get a function that takes the rest of the arguments". So for a function with two variables y^x, if y=2 is fixed, a function with a variable is obtained 2^x
Universal implementation
Select AllCopyput it in your notes .
function currying(fn) { var slice = Array.prototype.slice; var args = alice.call(arguments, 1); return function() { var innerArgs = slice.call(arguments); var finalArgs = args.concat(innerArgs); return fn.apply(null, finalArgs); }}
Let's look at a simple example
Select AllCopyput it in your notes .
function add(num1, num2) { return num1 + num2;}function curryAdd(num) { return num + 5;}add(2, 4); // 6curryAdd(1); // 6
curryAdd
Obviously not an implementation of the curry. But it is a good interpretation of the idea of curry.add 函数有两个参数,curryAdd 固定了其中一个参数
Use the above currying
function to construct the curryAdd
function.
Select AllCopyput it in your notes .
var curryAdd5 = currying(add, 5);var curryAdd4 = currying(add, 4);curryAdd5(1); // 6curryAdd4(1); // 5
Let's look at a classic Ajax example.
Select AllCopyput it in your notes .
functionAjax() {THIS.XHR =New XMLHttpRequest ();} Ajax.prototype.open =function(Type, URL, data, callback) {This.onload =function() {Callback (this.xhr.responsetext, this.xhr.status, THIX.XHR);} this.xhr.open (type, URL, data.async); this.xhr.send (Data.paras);} [ ' get ', ' post '].foreach (function (type) {Ajax.prototype[type] = currying ( Ajax.prototype.open, type);}) var xhr = new Ajax (); Xhr.get ( "/ Articles/list ', {}, function ( datas) {}); Xhr.post (function
get
post
two methods are derived from the ' open ' method.
From a common open function (which can accept any type), Curry becomes a dedicated function get, post.
Fixed variable factor
Fix the variable factor in advance to generate a more explicit application function, the most typical code is the Function.prototype.bing function defined by ES5
Select AllCopyput it in your notes .
Function.prototype.bing = function(context) { var _this = this, slice = Array.prototype.slice, _args = slice.call(arguments, 1); return function() { return _this.apply(context, _args.concat(slice.call(arguments))) }}
Deferred execution
Constantly curry, accumulate incoming parameters, and finally execute.
Select AllCopyput it in your notes .
functionAdd() {var sum =0, I, Len;for (i =0, Len =Arguments.length; i < Len; i++) {sum + =Arguments[i]; }return sum;}var currying =function(FN) {var _args = [];Returnfunctioncb () {if ( arguments.length = = = 0) {return Fn.apply (this, _args);} array.prototype.push.apply (_args, arguments); return CB; }}var Curryingadd = currying (add); Curryingadd (1) (2) (3) (4) (); //10var add321 = Curryingadd (3) ( Span class= "Hljs-number" >2, 1), add321 (4) (); //
Performance
Curry will certainly have some overhead (function nesting, which accounts for more memory than normal functions), but the performance bottleneck comes first from other causes (DOM operations, etc.).
From another point of view, whether you use the thought of not using curry, your code is likely to have entered a more complex pattern, and there will be greater overhead.
Some of the things about performance:
Accessing arguments objects is usually slower than accessing named parameters.
Some older browsers are quite slow in arguments.length implementations.
Using Fn.apply () and Fn.call () is slower than calling FN () directly.
Creating a large number of nested scopes and closures can lead to overhead, both content and speed.
Most bottlenecks come from DOM operations
Summarize
The Haskell Gari is named after the logic family,
As it is named, the function of the curry gives us a logical way of thinking about solving the problem.
[Turn]js function into function currying