diff --git a/solutions/elixir/space-age/1/lib/space_age.ex b/solutions/elixir/space-age/1/lib/space_age.ex new file mode 100644 index 0000000..66411e0 --- /dev/null +++ b/solutions/elixir/space-age/1/lib/space_age.ex @@ -0,0 +1,28 @@ +defmodule SpaceAge do + @seconds_in_earth_year 365.25 * 24 * 60 * 60 + + @type planet :: + :mercury + | :venus + | :earth + | :mars + | :jupiter + | :saturn + | :uranus + | :neptune + + @doc """ + Return the number of years a person that has lived for 'seconds' seconds is + aged on 'planet', or an error if 'planet' is not a planet. + """ + @spec age_on(planet, pos_integer) :: {:ok, float} | {:error, String.t()} + def age_on(:mercury, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 0.2408467)} + def age_on(:venus, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 0.61519726)} + def age_on(:earth, seconds), do: {:ok, seconds / @seconds_in_earth_year} + def age_on(:mars, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 1.8808158)} + def age_on(:jupiter, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 11.862615)} + def age_on(:saturn, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 29.447498)} + def age_on(:uranus, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 84.016846)} + def age_on(:neptune, seconds), do: {:ok, seconds / (@seconds_in_earth_year * 164.79132)} + def age_on(_, _), do: {:error, "not a planet"} +end