This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
5. Currying
Currying is the technique of turning a function that takes multiple arguments into a single argument, and returning a new function that takes the remaining arguments and returns a result.
In short:
- Managed by closures
- Support for chained calls
- Each run returns a function
That is: the technique of returning a new function each run by replacing multiple arguments with a single argument
5.1 Examples of Currying
// The normal add function
function add (a, b) {
return a + b;
}
add(1.2);
Copy the code
// Corylized function
function curryingAdd (x) {
return function(y) {
return x + y;
}
}
curryingAdd(x)(y);
Copy the code
5.2 Benefits of colitisation
- Parameters of reuse
- Confirmation in advance
- Delay to run
// Check the number
let numberReg = /[0-9]+/g;
// Verify lowercase letters
let stringReg = /[a-z]+/g;
/ / after currying
function curryingCheck(reg) {
return function(txt) {
returnreg.test(txt); }}let checkNumber = curryingCheck(numberReg);
let checkString = curryingCheck(stringReg);
console.log(checkNumber('13888888888')); // true
console.log(checkString('jsliang')); // true
Copy the code
5.3 Currying implementationadd(1)(2)(3)
// Implement an ADD method so that the results meet the following expectations
add(1) (2) (3) = 6;
add(1.2.3) (4) = 10;
add(1) (2) (3) (4) (5) = 15;
Copy the code
function add () {
const numberList = Array.from(arguments);
// Collect the remaining parameters further
const calculate = function() { numberList.push(... arguments);return calculate;
}
// Use an implicit toString conversion, which is performed at the end of execution
calculate.toString = function() {
return numberList.reduce((a, b) = > a + b, 0);
}
return calculate;
}
// Implement an ADD method so that the results meet the following expectations
console.log(add(1) (2) (3)); / / 6
console.log(add(1.2.3) (4)); / / 10;
console.log(add(1) (2) (3) (4) (5)); / / 15;
console.log(add(1) (2) (3) (4) (5.6.7)); / / 28
Copy the code
Compose (foo, bar, baz)(‘start’)
function foo(. args) {
console.log(args[0]);
return 'foo';
}
function bar(. args) {
console.log(args[0]);
return 'bar';
}
function baz(. args) {
console.log(args[0]);
return 'baz';
}
function compose() {
// Closure element - function list
const list = Array.from(arguments);
// Closure element - function list execution location
let index = -1;
// Closure element - return of the previous function
let prev = ' ';
// Return the closure
const doNext = function() {
index++; // Add index values
// Get the value of the second parenthesis when there is no previous element to start with
if(! prev) { prev =arguments[0];
}
// Set the previous result to be returned by the current function
prev = list[index](prev);
// Call recursively
if (index < list.length - 1) {
doNext(index + 1); }};// Return the closure the first time
return doNext;
}
compose(foo, bar, baz)('start');
Copy the code
The best articles
- Detail JS function currying
- Write the add function and add(1)(2)(3)(4) output 10. Then consider extensibility
- Discover the power of closures in JavaScript
- The underlying mechanism by which JavaScript closures work
- I never understood JavaScript closures until someone explained them to me like this
- Discover the power of closures in JavaScript
- The underlying mechanism by which JavaScript closures work
- I never understood JavaScript closures until someone explained them to me like this…
- Crack the front-end interview (80% fail) : Start with closures