# Practical introduction to Functional Programming with JS

AndreaZanin
35.2K views

## Currying

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. Let's dive straight into code:

Get the even numbers in an array
// standard way
return x + y + z;
}
// curried style
const add2 = x => // if you have only one argument in the lambda function you can omit the parenthesis
y =>
z =>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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.

E.g.

Generic filter callback to check any property of an object
const checkProperty = propertyName =>
expectedValue =>
object =>
object[propertyName] === expectedValue;
const candies = [
{"soft":true, "flavour":"strawberry"},
{"soft":false, "flavour":"strawberry"},
{"soft":false, "flavour":"cherry"},
{"soft":true, "flavour":"orange"},
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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 (a 2$item with a 0.1 discount will cost 1.8$) and an item, and that returns the item with the correct price.
E.g.

const item = {
"name": "Biscuits",
"type": "regular",
"category": "food",
"price": 2.0
}

applyCoupon("food")(0.1)(item).price === 1.8


Remember to apply the discount only to the right items!

Implement applyCoupon as a curriable function
const applyCoupon = // this should be a curried function
// { autofold
module.exports = {
applyCoupon: applyCoupon
};
// }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants