Created
June 14, 2020 17:21
-
-
Save CodHeK/7ccd2098235ccead736b04912a81e284 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const curry = (func) => { | |
const arity = func.length; // number of arguments | |
return build = (...args) => { | |
if(args.length >= arity) { // (1) | |
return func(...args); // (3) | |
} | |
else { | |
return build.bind(null, ...args); // (2) | |
} | |
} | |
} | |
const logger = (type, lineNumber, message) => { | |
console.log(`${type} : ${message} at line ${lineNumber}`); | |
} | |
const customLogger = curry(logger); | |
/* | |
Create partials with fixed first argument | |
*/ | |
const errorLogger = customLogger('ERROR'); | |
const warningLogger = customLogger('WARNING'); | |
/* | |
Use them like this with fixed first argument. | |
*/ | |
errorLogger(35, 'Some error message'); // ERROR : Some error message at line 35 | |
warningLogger(24, 'Some warning message'); // WARNING : Some warning message at line 24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment