Cheapest Item: Exercise

Write a point-free function to return a cart’s cheapest item.

Given a cart, return the cheapest item’s name.
Usage

  1. const cheapestItem = getCheapestItem(cart); // 'apple'

Your solution must be point-free.

  1. export default [{
  2. name: 'apple',
  3. price: 230
  4. }, {
  5. name: 'lettuce',
  6. price: 345
  7. }, {
  8. name: 'tomato',
  9. price: 13
  10. }, {
  11. name: 'lemon',
  12. price: 100
  13. }, {
  14. name: 'kiwi',
  15. price: 85
  16. }, {
  17. name: 'broccoli',
  18. price: 130
  19. }, {
  20. name: 'mango',
  21. price: 234983428
  22. }, {
  23. name: 'pear',
  24. price: 105
  25. }, {
  26. name: 'carrot',
  27. price: 10
  28. }];
  1. import {head,compose,prop,sortBy} from 'ramda';
  2. import cart from './cart';
  3. const getCheapestItem = compose(
  4. prop('name'),
  5. head,
  6. sortBy(prop("price"))
  7. );

Cheapest Item: Solution Review

Solution review.
We’ll cover the following

Sorting #

Getting the cheapest item implies you’re sorting by price. After that grab the item’s name.
A vanilla solution might look like this

  1. import cart from './cart';
  2. const getCheapestItem = (items) => {
  3. const byPriceAsc = items
  4. .sort((a, b) => a.price - b.price);
  5. const cheapest = byPriceAsc[0];
  6. return cheapest.name;
  7. };
  8. const result = getCheapestItem(cart);
  9. console.log({ result });

A $10 carrot is the cheapest item. What kind of grocery store is this?!
Anyways, we see the order of operations

  1. Sort by price
  2. Grab the first or last item (depending on how you sorted)
  3. Return its name

We know pipe and prop from the last exercise and those seem like good candidates here. Ramda also carries a sort function.

  1. import { pipe, prop, sort } from 'ramda';
  2. import cart from './cart';
  3. const getCheapestItem = pipe(
  4. sort((a, b) => a.price - b.price),
  5. (list) => list[0],
  6. prop('name')
  7. );
  8. const result = getCheapestItem(cart);
  9. console.log({ result });

Much better. Pat yourself on the back if you got this.
But did you know about sortBy?

  1. import { head, pipe, prop, sortBy } from 'ramda';
  2. import cart from './cart';
  3. const getCheapestItem = pipe(
  4. sortBy(prop('price')),
  5. head,
  6. prop('name')
  7. );
  8. const result = getCheapestItem(cart);
  9. console.log({ result });

It takes a function that describes how the data should be sorted. Useful if your sorts involve any complex logic.
https://ramdajs.com/docs/#sortBy

https://ramdajs.com/docs/#head

headList

[a] → a | Undefined
String → String
Parameters
Added in v0.1.0
Returns the first element of the given list or string. In some libraries this function is named first.
See also tail, init, last.

  1. R.head(['fi', 'fo', 'fum']); //=> 'fi'
  2. R.head([]); //=> undefined
  3. R.head('abc'); //=> 'a'
  4. R.head(''); //=> ''

https://ramdajs.com/docs/#tail

tailList

[a] → [a]
String → String
Parameters
Added in v0.1.0
Returns all but the first element of the given list or string (or object with a tail method).
Dispatches to the slice method of the first argument, if present.
See also head, init, last.

  1. R.tail([1, 2, 3]); //=> [2, 3]
  2. R.tail([1, 2]); //=> [2]
  3. R.tail([1]); //=> []
  4. R.tail([]); //=> []
  5. R.tail('abc'); //=> 'bc'
  6. R.tail('ab'); //=> 'b'
  7. R.tail('a'); //=> ''
  8. R.tail(''); //=> ''

https://ramdajs.com/docs/#init

initList

[a] → [a]
String → String
Parameters
Added in v0.9.0
Returns all but the last element of the given list or string.
See also last, head, tail.

  1. R.init([1, 2, 3]); //=> [1, 2]
  2. R.init([1, 2]); //=> [1]
  3. R.init([1]); //=> []
  4. R.init([]); //=> []
  5. R.init('abc'); //=> 'ab'
  6. R.init('ab'); //=> 'a'
  7. R.init('a'); //=> ''
  8. R.init(''); //=> ''

https://ramdajs.com/docs/#last

lastList

[a] → a | Undefined
String → String
Parameters
Added in v0.1.4
Returns the last element of the given list or string.
See also init, head, tail.

  1. R.last(['fi', 'fo', 'fum']); //=> 'fum'
  2. R.last([]); //=> undefined
  3. R.last('abc'); //=> 'c'
  4. R.last(''); //=> ''