Keyword lists are a key-value data structure.
[month: "April", year: 2018]
Keyword lists are lists of {key, value}
tuples, and can also be written as such, but the shorter syntax is more widely used.
[month: "April"] == [{:month, "April"}]
# => true
Keys in a keyword list must be atoms, but the values can be anything. Each key can be used more than once. The key-value pairs in a keyword list are ordered.
You can work with keyword lists using the same approaches as for lists, or you can use the Keyword
module.
You are the manager of a fancy restaurant that has a sizable wine cellar. A lot of your customers are demanding wine enthusiasts. Finding the right bottle of wine for a particular customer is not an easy task.
As a tech-savvy restaurant owner, you decided to speed up the wine selection process by writing an app that will let guests filter your wines by their preferences.
On the welcome screen of your app, you want to display a short explanation of each wine color.
Implement the WineCellar.explain_colors/0
function. It takes no arguments and returns a keyword list with wine colors as keys and explanations as values.
Color | Explanation |
---|---|
:white |
Fermented without skin contact. |
:red |
Fermented with skin contact using dark-colored grapes. |
:rose |
Fermented with some skin contact, but not enough to qualify as a red wine. |
A bottle of wine is represented as a 3-tuple of grape variety, year, and country of origin. The wines are stored by wine color in a keyword list.
[
white: {"Chardonnay", 2015, "Italy"},
white: {"Pinot grigio", 2017, "Germany"},
red: {"Pinot noir", 2016, "France"},
rose: {"Dornfelder", 2018, "Germany"}
]
Implement the WineCellar.filter/3
function. It should take a keyword list of wines, a color atom and a keyword list of options, with a default value of []
. The function should return a list of wines of a given color.
WineCellar.filter(
[
white: {"Chardonnay", 2015, "Italy"},
white: {"Pinot grigio", 2017, "Germany"},
red: {"Pinot noir", 2016, "France"},
rose: {"Dornfelder", 2018, "Germany"}
],
:white
)
# => [
# {"Chardonnay", 2015, "Italy"},
# {"Pinot grigio", 2017, "Germany"}
# ]
Extend the WineCellar.filter/3
function. When given a :year
option, the function should return a list of wines of a given color from a given year.
Use the already-implemented WineCellar.filter_by_year/2
function. It takes a list of wines and a year as arguments and returns a list of wines from a given year.
WineCellar.filter(
[
white: {"Chardonnay", 2015, "Italy"},
white: {"Pinot grigio", 2017, "Germany"},
red: {"Pinot noir", 2016, "France"},
rose: {"Dornfelder", 2018, "Germany"}
],
:white,
year: 2015
)
# => [
# {"Chardonnay", 2015, "Italy"}
# ]
Extend the WineCellar.filter/3
function. When given a :country
option, the function should return a list of wines of a given color from a given country.
Use the already-implemented WineCellar.filter_by_country/2
function. It takes a list of wines and a country as arguments and returns a list of wines from a given country.
Make sure that the function works when given both the :year
and the :country
option, in any order.
WineCellar.filter(
[
white: {"Chardonnay", 2015, "Italy"},
white: {"Pinot grigio", 2017, "Germany"},
red: {"Pinot noir", 2016, "France"},
rose: {"Dornfelder", 2018, "Germany"}
],
:white,
year: 2015,
country: "Germany"
)
# => []
Sign up to Exercism to learn and master Elixir with 57 concepts, 159 exercises, and real human mentoring, all for free.