const loggerHelper = (mode,initialMessage,errorMessage,lineNo)
=> {
if(mode === "DEBUG")
console.debug(initialMessage,errorMessage +
"at line: " + lineNo)
else if(mode === "ERROR")
console.error(initialMessage,errorMessage +
"at line: " + lineNo)
else if(mode === "WARN")
console.warn(initialMessage,errorMessage +
"at line: " + lineNo)
else
throw "Wrong mode"
}
//The developer used to call the function:
loggerHelper("ERROR","Error At Stats.js","Invalid argument passed",23)
//Now let’s solve the repeating first two arguments problem via curry:
let errorLogger = curry(loggerHelper)("ERROR")("Error At Stats.js");
let debugLogger = curry(loggerHelper)("DEBUG")("Debug At Stats.js");
let warnLogger = curry(loggerHelper)("WARN")("Warn At Stats.js");
//Now we can easily refer to the earlier curried functions and use them under the respective context:
//for error
errorLogger("Error message",21)
=> Error At Stats.js Error messageat line: 21
//for debug
debugLogger("Debug message",233)
=> Debug At Stats.js Debug messageat line: 233
//for warn
warnLogger("Warn message",34)
=> Warn At Stats.js Warn messageat line: 34
A logger Function: Using Currying The example in the previous section helped us understand what currying does, but let’s use a more complicated example in this section. As developers when we write code, we do a lot of logging at several stages of the application. We could write a helper logger function that looks like.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.