> For the complete documentation index, see [llms.txt](https://elixir.petrolidas.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://elixir.petrolidas.com/exhort/day-14.md).

# Day 14

## Exercises

* [x] [Boutique Inventory](#boutique-inventory)

### Boutique Inventory

[Boutique Inventory in Elixir on Exercism](https://exercism.org/tracks/elixir/exercises/boutique-inventory) **|** [View my solution](https://exercism.org/tracks/elixir/exercises/boutique-inventory/solutions/petros)

#### Solution

<details>

<summary>Expand to see code (spoiler alert)</summary>

{% code lineNumbers="true" %}

```elixir
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
```

{% endcode %}

[View gist on GitHub](https://gist.github.com/petros/809098bdb9b12a6b3def38b291e7f05e)

</details>

#### Notes

Oh Zeus. I really struggled with this one. And it was so simple. I don't know why. Here's my accepted solution:

```elixir
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
```

I was trying all sorts of irrelevant things :grimacing:. I was also thrown off by the mention of `Enum.into` in `README.md`. I guess it was an alternative to `Map.new`?\
\
For `total_quantity/1`, I used a different approach, while the exercise recommended `Enum.reduce`. It was the purpose of the exercise to teach you that. My initial implementation was:

```elixir
item.quantity_by_size
|> Map.values
|> Enum.sum
```

I think it's a little easier in the eyes, but I have no idea if it's slower as I am calling a `Map` function and then an `Enum` function. Whereas `reduce/3` is one function.

## Overall progress

<figure><img src="/files/UQqPBFzpi31rjQ2mdvsx" alt="An image showing my progress on Exercism. It&#x27;s 15.4% as of Sunday, 21 August 2022."><figcaption><p>Progress</p></figcaption></figure>
