module Data.Distance
( Distance
, meters, centimeters, millimeters, kilometers
, toMeters, toCentimeters, toMillimeters, toKilometers
)
where
newtype Distance
= Distance { unDistance :: Double }
deriving (Show, Eq, Ord)
instance Monoid Distance where
mempty = Distance 0
mappend (Distance a) (Distance b) = Distance (a + b)
meters :: Double -> Distance
meters = Distance
centimeters :: Double -> Distance
centimeters = meters . (/ 100)
millimeters :: Double -> Distance
millimeters = centimeters . (/ 10)
kilometers :: Double -> Distance
kilometers = meters . (* 1000)
toMeters :: Distance -> Double
toMeters = unDistance
toCentimeters :: Distance -> Double
toCentimeters = (*100) . toMeters
toMillimeters :: Distance -> Double
toMillimeters = (*10) . toCentimeters
toKilometers :: Distance -> Double
toKilometers = (/1000) . toMeters