Tracks
/
Elixir
Elixir
/
Exercises
/
Kitchen Calculator
Kitchen Calculator

Kitchen Calculator

Learning Exercise

While completing Kitchen Calculator, you'll learn 2 concepts

Instructions

While preparing to bake cookies for your friends, you have found that you have to convert some of the measurements used in the recipe. Being only familiar with the metric system, you need to come up with a way to convert common US baking measurements to milliliters (mL) for your own ease.

Use this conversion chart for your solution:

Unit to convert volume in milliliters (mL)
mL 1 1
US cup 1 240
US fluid ounce 1 30
US teaspoon 1 5
US tablespoon 1 15

Being a talented programmer in training, you decide to use milliliters as a transition unit to facilitate the conversion from any unit listed to any other (even itself).

1. Get the numeric component from a volume-pair

Implement the KitchenCalculator.get_volume/1 function. Given a volume-pair tuple, it should return just the numeric component.

KitchenCalculator.get_volume({:cup, 2.0})
# => 2.0

2. Convert the volume-pair to milliliters

Implement the KitchenCalculator.to_milliliter/1 function. Given a volume-pair tuple, it should convert the volume to milliliters using the conversion chart.

Use multiple function clauses and pattern matching to create the functions for each unit. The atoms used to denote each unit are: :cup, :fluid_ounce, :teaspoon, :tablespoon, :milliliter. Return the result of the conversion wrapped in a tuple.

KitchenCalculator.to_milliliter({:cup, 2.5})
# => {:milliliter, 600.0}

3. Convert the milliliter volume-pair to another unit

Implement the KitchenCalculator.from_milliliter/2 function. Given a volume-pair tuple and the desired unit, it should convert the volume to the desired unit using the conversion chart.

Use multiple function clauses and pattern matching to create the functions for each unit. The atoms used to denote each unit are: :cup, :fluid_ounce, :teaspoon, :tablespoon, :milliliter

KitchenCalculator.from_milliliter({:milliliter, 1320.0}, :cup)
# => {:cup, 5.5}

4. Convert from any unit to any unit

Implement the KitchenCalculator.convert/2 function. Given a volume-pair tuple and the desired unit, it should convert the given volume to the desired unit.

KitchenCalculator.convert({:teaspoon, 9.0}, :tablespoon)
# => {:tablespoon, 3.0}
Last updated 29 September 2022
Edit via GitHub The link opens in a new window or tab
Elixir Exercism

Ready to start Kitchen Calculator?

Sign up to Exercism to learn and master Elixir with 57 concepts, 156 exercises, and real human mentoring, all for free.