Total Cart Price: Solution Review
Solution review.
We'll cover the following...
Getting Prices
Since point-free takes some getting used to, it can help to first write a plain solution and make it point-free later.
If you need a cart’s total price and each item’s shaped like this…
{
name: 'apples',
price: 2.49
}
…start by planning how you’d initially prepare the data. It’s a collection so map can get the prices and reduce can add them up for us.
A Touch of Formatting
We’d like a dollar figure, so 36.410000000000004 should be $36.41.
Refactoring to Point-Free
What were our steps?
- Get each price
- Add them up
- Format as dollars (USD)
We know pipe's great for multiple steps, so that’ll definitely help us get a point-free solution. And map/reduce are provided by Ramda.
Even though the map/reduce functions aren’t point-free, getTotalPrice is. This solution’s perfectly fine so pat yourself on the back if you got it.
We can make map/reduce point-free though. Ramda’s prop function grabs a given property of an object.
map(prop('price'))
And Ramda’s add function can be put inside reduce.
reduce(add, 0);
Wait There’s More!
Plucking a property and summing a list are such common use cases that Ramda provides functions for them!
Check out pluck and sum.
https://ramdajs.com/docs/#pluck
https://ramdajs.com/docs/#sum
I personally liked this solution the most as it’s so concise and expressive. If you got this one then double-pat yourself on the back.