-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wine Cellar.ex
42 lines (34 loc) · 1.38 KB
/
Wine Cellar.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
defmodule WineCellar do
@white_description "Fermented without skin contact."
@red_description "Fermented with skin contact using dark-colored grapes."
@rose_description "Fermented with some skin contact, but not enough to qualify as a red wine."
def explain_colors do
# Please implement the explain_colors/0 function
[
white: @white_description, red: @red_description, rose: @rose_description
]
end
def filter(cellar, color, opts \\ []) do
# Please implement the filter/3 function
wines = Keyword.get_values(cellar, color)
wines = if Keyword.has_key?(opts, :year), do: filter_by_year(wines, opts[:year]), else: wines
if Keyword.has_key?(opts, :country), do: filter_by_country(wines, opts[:country]), else: wines
end
# The functions below do not need to be modified.
defp filter_by_year(wines, year)
defp filter_by_year([], _year), do: []
defp filter_by_year([{_, year, _} = wine | tail], year) do
[wine | filter_by_year(tail, year)]
end
defp filter_by_year([{_, _, _} | tail], year) do
filter_by_year(tail, year)
end
defp filter_by_country(wines, country)
defp filter_by_country([], _country), do: []
defp filter_by_country([{_, _, country} = wine | tail], country) do
[wine | filter_by_country(tail, country)]
end
defp filter_by_country([{_, _, _} | tail], country) do
filter_by_country(tail, country)
end
end