🔹 Quick Byte: Understanding Currying in Java with a Real-Life Example 🔹
Currying: A technique in functional programming where a function is transformed into a series of functions, each taking a single argument. It breaks down a function that takes multiple arguments into a series of functions that each take one argument.
Real-Life Example: Discount Calculation in E-Commerce
Let’s consider an e-commerce application where we need to apply different discount strategies based on the user type (e.g., regular customer, member, VIP) and the product category (e.g., electronics, clothing). Here’s an example using currying to apply these discounts:
import java.util.function.Function;
public class CurryingExample {
// Curried function to apply discount based on user type and product category
public static Function<String, Function<String, Function<Double, Double>>> discountCalculator =
userType -> productCategory -> price -> {
double discount = 0.0;
// Apply discount based on user type
switch (userType) {
case "Regular":
discount += 0.05;
break;
case "Member":
discount += 0.10;
break;
case "VIP":
discount += 0.15;
break;
}
// Apply additional discount based on product category
switch (productCategory) {
case "Electronics":
discount += 0.05;
break;
case "Clothing":
discount += 0.10;
break;
}
// Calculate final price after applying discounts
return price * (1 - discount);
};
public static void main(String[] args) {
double finalPrice = discountCalculator.apply("Member").apply("Electronics").apply(200.0);
System.out.println("Final price after discount: $" + finalPrice); // Outputs: Final price after discount: $170.0
}
}
Explanation:
- Currying Function: discountCalculator takes userType, then productCategory, and finally the price to calculate the discounted price.
- Chained Application: The function is applied in a chained manner: discountCalculator.apply(“Member”).apply(“Electronics”).apply(200.0).
- Result: The final price after applying the discounts is calculated and printed.
Benefits of Currying in This Example:
- Modular Calculation: Discounts are calculated in a modular way, making it easy to add or change discount rules.
- Reusability: Partial functions (e.g., discounts for specific user types or product categories) can be reused with different prices.
- Readability: The code is more readable and maintainable due to the clear separation of concerns.
Read more: