Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.
This becomes very easy using lambda functions and closures. So let's dive straight into code:
So why is this useful? Because we are now able to pass the arguments at different points in time, this means that we can use currying to "construct" a function.
And now time to get your hands dirty: remember the
applyCoupon function we wrote in the previous chapter? It was very specific, now we want to create a curryable function that takes as arguments (in this order) category, discount between 0 and 1 (0.8 = 80% discount) and then an item and returns the item with the correct price.
Remember to apply the discount only to the right items!