Implementing Specials

Take a look at the implementation of the specials module.

The implementation

A simple method is to count how many of each item is in the list. Account for the specials first, reducing the count every time the specials apply, and then run over the list of items that are not on sale.

def total(item_list, price_list, specials) do
  counts = count_seen(item_list)
  {counts_left, prices} = apply_specials(counts, specials) 
  prices + apply_regular(counts_left, price_list)
end

Here, count_seen/1 should create a list of each item and how many times it’s been seen. We then pass that data to apply_specials/2, ...