Emre Yilmaz
1 min readFeb 18, 2020

--

Hi Pierre, the answer to your question is already there actually.

If you remember we have this function and we can use it as follows:

const createDiscountedProduct = product => ({  ...product,  discountedPrice: product.price * 0.8});const discountedProducts = products.map(createDiscountedProduct)

Instead, we can refactor it to have an additional parameter for a discount ratio:

const discountRatioThatComesFromSomewhere = 0.8;const createDiscountedProduct = discountRatio => product => ({  ...product,  discountedPrice: product.price * discountRatio});const discountedProducts = products.map(createDiscountedProduct(discountRatioThatComesFromSomewhere))

Better you can use a helper curry function from a library like Ramda to make your life easier:

const discountRatioThatComesFromSomewhere = 0.8;const createDiscountedProduct = R.curry((discountRatio, product) => ({  ...product, discountedPrice: product.price * discountRatio}));const discountedProducts = products.map(createDiscountedProduct(discountRatioThatComesFromSomewhere))

Feel free to create a new function with the discount ratio and give it a name. But this usage is also perfectly fine.

I hope this answers your question better.

Keep coding ✌️

--

--

Responses (1)