Skip to content

Instantly share code, notes, and snippets.

@CodHeK
Created June 14, 2020 17:21
Show Gist options
  • Save CodHeK/7ccd2098235ccead736b04912a81e284 to your computer and use it in GitHub Desktop.
Save CodHeK/7ccd2098235ccead736b04912a81e284 to your computer and use it in GitHub Desktop.
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