Search⌘ K
AI Features

Implementing Specials

Explore how to implement special pricing in property-driven development using PropEr with Elixir. Understand counting item occurrences, applying specials first, and calculating total prices. Learn helper functions and techniques like Enum.map_reduce for efficient list processing, followed by negative testing to ensure reliability.

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, ...