Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions solutions/elixir/space-age/1/lib/space_age.ex
Original file line number Diff line number Diff line change
@@ -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