exercism/elixir/kitchen-calculator/lib/kitchen_calculator.ex

24 lines
631 B
Elixir
Raw Permalink Normal View History

2023-12-18 02:09:15 +00:00
defmodule KitchenCalculator do
defp milliliter(:milliliter), do: 1
defp milliliter(:cup), do: 240
defp milliliter(:fluid_ounce), do: 30
defp milliliter(:teaspoon), do: 5
defp milliliter(:tablespoon), do: 15
def get_volume(_volume_pair = {_, volume}), do: volume
def to_milliliter(_volume_pair = {from_unit, volume}),
do:
{:milliliter, milliliter(from_unit) * volume}
def from_milliliter(_volume_pair = {:milliliter, volume}, to_unit),
do:
{to_unit, volume / milliliter(to_unit)}
def convert(volume_pair, unit) do
volume_pair
|> to_milliliter
|> from_milliliter(unit)
end
end