Record corrification, test their own learning level;

Definition: In computer science, Currying is a technique that converts a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returns a new function that takes the remaining arguments and returns a result. The technique was named after logician Haskell Curry by Christopher Strachey, although it was invented by Moses Schnfinkel and Gottlob Frege.

— Quoted from Baidu Baike

add(1)(2)(3)(4) = 10; Implement add function

  • The length of the parameter is certain (like the length of the parameter above is 4);
let sum = (a,b,c,d)=>a+b+c+d; function curry(sum){ let len = sum.length; // Let opts = []; Sum return function next(...) sum return function next(... Arg){// Collect parameter opts = opts.concat(arg); if(opts.length < len){ return next; } else { return sum.apply(null,opts); }}}; // let func = curry(sum); console.log(func(1)(2)(3)(4)); / / 10. The console log (func (1, 2) (3) (4)); / / 10Copy the code

Closure iterating closure iterating closure iterating is a mechanism for saving variables

  • Parameter length is variable. The boundary of () is similar to add(1)(2)(3)(4)();

To implement the

function curry2(fn) { let opts =[]; return function next(... If (args. Length){return next} else {return fn. Apply (null,opts); }}}; let add2 = function () { return [...arguments].reduce((prev,cur)=>prev+cur); }; let func2 = curry2(add2); console.log(func2(1)(2)(3)(4)()); / / 10. The console log (func2 (1) (2, 3) (4) the ()); / / 10Copy the code
  • The indeterminate boundary is similar to add(1)(2)(3)(4)… (101)…

How do we determine the boundary? That’s the hardest problem, but let’s do the implementation. Okay

function curry3(fn) { let opts = []; console.log('opts'); function next(... Args) {// next is only used to collect opts = opts.concat (args); Next.valueof = function () {return fn.apply(null, opts); } // Next.toString = function () {return fn.apply(null, opts); } return next; }; let add3 = function () { return [...arguments].reduce((prev,cur)=>prev+cur); }; let func3 = curry3(add3); func3(1)(2)(3)(4); console.log(func3(1)(2)(3)(4)); / / 10. The console log (func3 - a non-class function (1) (2, 3) (4)); / / 10Copy the code

Rewrote next’s toString and valueOf functions. Add3 is called when these two functions are called. ToString and valueOf are called implicitly.

When the function func3(1)(2)(3)(4); When this line of code is done, instead of the implicit call, console.log(func3(1)(2)(3)(4)) is executed, and toString is executed. I’m not sure why this is happening, but my understanding is that, Func3 (1)(2)(3)(4); func3(1)(2)(3)(4); func3(1)(3)(4); So instead of toString and valueOf, this line console.log(func3(1)(2)(3)(4)) we use, we pass the value into console.log(), we use this value, so we do toString and valueOf, Conclude that toString and valueOf are executed when values are used

Implicit invocation occurs whenever a value is used in the more common if criteria, such as type conversions, operators, and function arguments.

Recommended reading: Cremation and anti-Cremation

Limited level, simple implementation, unavoidable errors, welcome to point out.