호두나무 공방/Exercism in Elixir

Boutique Inventory - Exercism in Elixir

2022. 4. 29. 22:29

문제 보기

Enum 모듈을 배우는 문제였다. 개념 문제여서 딱히 코멘트할 내용은 없음!

defmodule BoutiqueInventory do
  def sort_by_price(inventory) do
    Enum.sort_by(inventory, &(&1.price))
  end

  def with_missing_price(inventory) do
    Enum.filter(inventory, &(is_nil(&1.price)))
  end

  def update_names(inventory, old_word, new_word) do
    Enum.map(inventory, fn %{name: name} = record ->
      %{record | name: String.replace(name, old_word, new_word)}
    end)
  end

  def increase_quantity(%{quantity_by_size: q} = item, count) do
      %{item | quantity_by_size: Map.new(q, fn {k, v} -> {k, v + count} end)}
  end

  def total_quantity(%{quantity_by_size: q}) do
      Enum.reduce(q, 0, fn {_k, v}, acc -> v + acc end)
  end
end