I was looking a bit into functional programming in order to learn new tricks and one thing that caught my attention is currying. I find the trick itself interesting but I wonder if it is really beneficial in any common scenario, especially given lambdas.
I wrote a bit of code to showcase my point a bit better.
def calc_price(amount, price_per_unit, delivery_price):
return amount * price_per_unit + delivery_price
def curry_calc_price(amount):
def add_price(price_per_unit):
def add_delivery(delivery_price):
return calc_price(amount, price_per_unit, delivery_price)
return add_delivery
return add_price
# curry way
ten_pieces = curry_calc_price(10)
ten_pieces_five_each_five_delivery = ten_pieces(5)(5)
# lambda way
ten_pieces_five_each_lambda = lambda delivery: calc_price(10, 5, delivery)
ten_pieces_five_each_five_delivery_lambda = ten_pieces_five_each_lambda(5)
print(ten_pieces_five_each_five_delivery)
print(ten_pieces_five_each_five_delivery_lambda)
The output is the same and seems to me use simplicity is the same too. I genuinely want to know the benefits of currying and how to use it right.