Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
You're going to create a Products
module that mimics searching a list of products.
Create a Products.all_items/0
function that uses comprehensions to generate variations of items.
items should be a map with the following fields:
- name: a string of either
"computer"
,"phone"
or"tablet"
- price: A random integer between
1
and100
.
Using these values above, all_items/0
should generate 300
unique items.
[
%{name: "computer", price: 1},
%{name: "computer", price: 2},
%{name: "computer", price: 3}
...
] = Products.all_items()
Given a list of items and a keyword list of filters, use comprehensions to filter the list of items that match the given filters.
- min: The product price must be greater than or equal to this value.
- max: The product must be less than or equal to this value.
- name: The product name must be equal to this value.
all_items = Products.all_items()
[
%{name: "computer", price: 10},
%{name: "computer", price: 11},
%{name: "computer", price: 12},
%{name: "computer", price: 13},
%{name: "computer", price: 14},
%{name: "computer", price: 15},
] = Products.search(all_items, [min: 10, max: 15, name: "computer"])
If a filter is not provided, then do not filter based on that value.
[
%{name: "computer", price: 10},
%{name: "phone", price: 10},
%{name: "tablet", price: 10},
] = Products.search(all_items, [min: 10, max: 10])
Solution Example
defmodule Products do
def all_items do
for name <- ["computer", "phone", "tablet"], price <- 1..100 do
%{name: name, price: price}
end
end
def search(items, filters) do
name = Keyword.get(filters, :name)
min = Keyword.get(filters, :min)
max = Keyword.get(filters, :max)
for item <- items,
!name || item.name == name,
!max || item.price <= max,
!min || min <= item.price do
item
end
end
end
defmodule Products do
def all_items do
end
def search(items, filters) do
end
end
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Comprehension Product Search exercise"
$ git push
We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.