Skip to content

Instantly share code, notes, and snippets.

@petros
Last active August 23, 2022 08:04
Show Gist options
  • Save petros/809098bdb9b12a6b3def38b291e7f05e to your computer and use it in GitHub Desktop.
Save petros/809098bdb9b12a6b3def38b291e7f05e to your computer and use it in GitHub Desktop.
Boutique Inventory - Elixir - Exercism
defmodule BoutiqueInventory do
def sort_by_price(inventory) do
inventory
|> Enum.sort_by(&(&1.price))
end
def with_missing_price(inventory) do
inventory
|> Enum.filter(&is_nil(&1.price))
end
def update_names(inventory, old_word, new_word) do
inventory
|> Enum.map(&replace_word(&1, old_word, new_word))
end
defp replace_word(map, old_word, new_word) do
name = String.replace(map.name, old_word, new_word)
Map.put(map, :name, name)
end
def increase_quantity(item, count) do
qbs = Map.new(item.quantity_by_size, fn {k, v} -> {k, v + count} end)
%{item | quantity_by_size: qbs }
end
def total_quantity(item) do
item.quantity_by_size
|> Enum.reduce(0, fn {_, v}, sum -> sum + v end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment