// Currying function for calculating the total price
function calculateTotal(price) {
return function(tax) {
return function(quantity) {
return price * quantity * (1 + tax);
};
};
}
// Create a curried function for a specific product
const calculateTotalForProduct = calculateTotal(10.99); // Set the base price
// Calculate the total price for a specific product with 8% tax and 3 quantity
const total = calculateTotalForProduct(0.08)(3);
console.log(`Total Price: $${total.toFixed(2)}`);
function sum(a, b, c) {
return a + b + c;
}
// Curried version of the sum function
function curriedSum(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
// Using the curried function
console.log(curriedSum(1)(2)(3)); // Outputs: 6
// Another practial example
function log(date) {
return function(importance) {
return function(message) {
console.log(`[${date.getHours()}:${date.getMinutes()}] [${importance}] ${message}`);
};
};
}
const logNow = log(new Date());
const logInfoNow = logNow("INFO");
logInfoNow("This is an informational message.");