Skip to content

Instantly share code, notes, and snippets.

@vKxni
Created September 25, 2022 19:50
Show Gist options
  • Save vKxni/acd85001f818b2761eb95a4460d541a1 to your computer and use it in GitHub Desktop.
Save vKxni/acd85001f818b2761eb95a4460d541a1 to your computer and use it in GitHub Desktop.
Calculate stuff for Kitchen
defmodule KitchenCalculator do
def get_volume(volume_pair) do
elem(volume_pair, 1)
end
def to_milliliter({:milliliter, volume}), do: {:milliliter, volume}
def to_milliliter({:cup, volume}), do: {:milliliter, volume * 240}
def to_milliliter({:fluid_ounce, volume}), do: {:milliliter, volume * 30}
def to_milliliter({:teaspoon, volume}), do: {:milliliter, volume * 5}
def to_milliliter({:tablespoon, volume}), do: {:milliliter, volume * 15}
def from_milliliter({:milliliter, volume}, :milliliter), do: {:milliliter, volume}
def from_milliliter({:milliliter, volume}, :cup), do: {:cup, volume / 240}
def from_milliliter({:milliliter, volume}, :fluid_ounce), do: {:fluid_ounce, volume / 30}
def from_milliliter({:milliliter, volume}, :teaspoon), do: {:teaspoon, volume / 5}
def from_milliliter({:milliliter, volume}, :tablespoon), do: {:tablespoon, volume / 15}
def convert(volume_pair, unit) do
volume_pair
|> to_milliliter
|> from_milliliter(unit)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment