- Cheapest Item: Exercise
- Cheapest Item: Solution Review
- https://ramdajs.com/docs/#head">https://ramdajs.com/docs/#head
- https://ramdajs.com/docs/#tail">https://ramdajs.com/docs/#tail
- https://ramdajs.com/docs/#init">https://ramdajs.com/docs/#init
- https://ramdajs.com/docs/#last">https://ramdajs.com/docs/#last
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
const cheapestItem = getCheapestItem(cart); // 'apple'
Your solution must be point-free.
export default [{name: 'apple',price: 230}, {name: 'lettuce',price: 345}, {name: 'tomato',price: 13}, {name: 'lemon',price: 100}, {name: 'kiwi',price: 85}, {name: 'broccoli',price: 130}, {name: 'mango',price: 234983428}, {name: 'pear',price: 105}, {name: 'carrot',price: 10}];
import {head,compose,prop,sortBy} from 'ramda';import cart from './cart';const getCheapestItem = compose(prop('name'),head,sortBy(prop("price")));
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
import cart from './cart';const getCheapestItem = (items) => {const byPriceAsc = items.sort((a, b) => a.price - b.price);const cheapest = byPriceAsc[0];return cheapest.name;};const result = getCheapestItem(cart);console.log({ result });
A $10 carrot is the cheapest item. What kind of grocery store is this?!
Anyways, we see the order of operations
- Sort by price
- Grab the first or last item (depending on how you sorted)
- 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.
import { pipe, prop, sort } from 'ramda';import cart from './cart';const getCheapestItem = pipe(sort((a, b) => a.price - b.price),(list) => list[0],prop('name'));const result = getCheapestItem(cart);console.log({ result });
Much better. Pat yourself on the back if you got this.
But did you know about sortBy?
import { head, pipe, prop, sortBy } from 'ramda';import cart from './cart';const getCheapestItem = pipe(sortBy(prop('price')),head,prop('name'));const result = getCheapestItem(cart);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.
R.head(['fi', 'fo', 'fum']); //=> 'fi'R.head([]); //=> undefinedR.head('abc'); //=> 'a'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.
R.tail([1, 2, 3]); //=> [2, 3]R.tail([1, 2]); //=> [2]R.tail([1]); //=> []R.tail([]); //=> []R.tail('abc'); //=> 'bc'R.tail('ab'); //=> 'b'R.tail('a'); //=> ''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.
R.init([1, 2, 3]); //=> [1, 2]R.init([1, 2]); //=> [1]R.init([1]); //=> []R.init([]); //=> []R.init('abc'); //=> 'ab'R.init('ab'); //=> 'a'R.init('a'); //=> ''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.
R.last(['fi', 'fo', 'fum']); //=> 'fum'R.last([]); //=> undefinedR.last('abc'); //=> 'c'R.last(''); //=> ''
