Generating Specials
Take a look at how to generate the list of specials, regular items and items on the specials.
Generating the list of specials
We’ll start with the list of specials since it’s conceptually simpler and a prerequisite of other generators. The method here is to first extract the list of all item names from the price list, and then create a matching table of specials. Let’s take a look at how it can be done.
defp special_list(price_list) do
items = for {name, _} <- price_list, do: name
let specials <- list({elements(items), choose(2, 5), integer()}) do
sorted = Enum.sort(specials)
Enum.dedup_by(sorted, fn {x, _, _} -> x end)
end
end
The generated list of specials contains the name of the item, how many of them are required for the special to apply, and then the price for the whole group. The specials are generated from randomly selected entries from the item list with the duplicates removed, so that there is always a matching non-special item to any special one. Note that the special price can be higher than the non-special price with this generator.
Generating the list of regular items
It is a bit tricky to generate the list of items that are never special. We’ll call them regular items. We have to make it so we generate zero or more items, as long as the maximal value is below any special discount values for the item. Let’s take a look at how it can be done.
Get hands-on with 1200+ tech skills courses.