Skip to content

Space Age

Given an age in seconds, say how old someone would be on each planet of the Solar System.

Adapted from Exercism’s problem specification (MIT). Listed under Numbers & Math.

// FILE: space_age.nomi
//# How old someone would be on each planet of the Solar System.

once earth_year_seconds = 31_557_600.0

/// A planet of the Solar System.
pub enum Planet
    Mercury
    Venus
    Earth
    Mars
    Jupiter
    Saturn
    Uranus
    Neptune
end

impl Planet
    /// The planet named `name`, such as `"Mars"`, or `None` for a name that
    /// is not one of the eight.
    pub fn parse(name: String) -> Maybe<Planet>
        Planet.variants() |> Iter.find(&("${&1}" == name))
    end

    /// How long the planet takes to orbit the Sun, in Earth years.
    pub fn orbital_period(planet: Planet) -> Float
        case planet
            .Mercury -> 0.2408467
            .Venus   -> 0.61519726
            .Earth   -> 1.0
            .Mars    -> 1.8808158
            .Jupiter -> 11.862615
            .Saturn  -> 29.447498
            .Uranus  -> 84.016846
            .Neptune -> 164.79132
        end
    end
end

derive Display for Planet
derive Variants for Planet

/// The age, in the planet's years, of someone `seconds` old.
pub fn age(planet: Planet, seconds: Int) -> Float
    earth_years = Int.to_float(seconds) / earth_year_seconds
    earth_years / Planet.orbital_period(planet)
end

// FILE: space_age_test.nomi
import space_age.{self, Planet}

test "age"
    [
        ("age on Earth",   Planet.Earth, 1000000000, 31.69),
        ("age on Mercury", .Mercury,     2134835688, 280.88),
        ("age on Venus",   .Venus,       189839836,  9.78),
        ("age on Mars",    .Mars,        2129871239, 35.88),
        ("age on Jupiter", .Jupiter,     901876382,  2.41),
        ("age on Saturn",  .Saturn,      2000000000, 2.15),
        ("age on Uranus",  .Uranus,      1210123456, 0.46),
        ("age on Neptune", .Neptune,     1821023456, 0.35),
    ]
    |> subtests (description, planet, seconds, expected), description
        assert space_age.age(planet, seconds) |> Float.round(2) == expected
    end
end

// Not canonical: the canonical data names planets only as inputs to `age`.
test "planet names"
    assert Planet.parse("Neptune") == Some(.Neptune)
end

test "invalid planet causes error"
    assert Planet.parse("Sun") == None
end
  • The planets are a Planet enum, and age takes one, so an age on a planet that does not exist cannot be asked for. The canonical “not a planet” case becomes Planet.parse("Sun") == None; the test file adds one non-canonical case that parse finds a planet.
  • Planet.parse finds the variant whose derive Display name matches, over derive Variants’ list, instead of a case with eight string arms that repeat the variant names.
  • Planet.parse returns a Maybe, not a Result: a name either names a planet or it does not, and there is only one way to fail.
  • The orbital periods are a case in Planet.orbital_period, so adding a planet without its period is a missing arm.
  • The canonical ages are given to two decimal places, so the tests round the computed age with Float.round(age, 2) and compare exactly. The rounded Float is the one nearest the two-decimal value, which is the Float the expected literal reads as.
  • The table’s first row names Planet.Earth; later rows write bare variants, which take the enum of the first row’s column.