module Data.Geo.Jord.Speed
(
Speed
, average
, metresPerSecond
, kilometresPerHour
, milesPerHour
, knots
, feetPerSecond
, speed
, read
, toMetresPerSecond
, toKilometresPerHour
, toMilesPerHour
, toKnots
, toFeetPerSecond
, add
, subtract
, zero
) where
import Control.Applicative ((<|>))
import Prelude hiding (read, subtract)
import Text.ParserCombinators.ReadP (ReadP, pfail, readP_to_S, skipSpaces, string)
import Text.Read (readMaybe)
import Data.Geo.Jord.Duration (Duration)
import qualified Data.Geo.Jord.Duration as Duration (toHours)
import Data.Geo.Jord.Length (Length)
import qualified Data.Geo.Jord.Length as Length (toMillimetres)
import Data.Geo.Jord.Parser
newtype Speed =
Speed
{ mmPerHour :: Int
}
deriving (Eq)
instance Read Speed where
readsPrec _ = readP_to_S speed
instance Show Speed where
show s = show (toKilometresPerHour s) ++ "km/h"
instance Ord Speed where
(<=) (Speed s1) (Speed s2) = s1 <= s2
add :: Speed -> Speed -> Speed
add a b = Speed (mmPerHour a + mmPerHour b)
subtract :: Speed -> Speed -> Speed
subtract a b = Speed (mmPerHour a - mmPerHour b)
zero :: Speed
zero = Speed 0
average :: Length -> Duration -> Speed
average d t = Speed (round (mm / h))
where
mm = Length.toMillimetres d
h = Duration.toHours t
metresPerSecond :: Double -> Speed
metresPerSecond mps = Speed (round (mps * 3600000.0))
kilometresPerHour :: Double -> Speed
kilometresPerHour kph = Speed (round (kph * 1e+6))
milesPerHour :: Double -> Speed
milesPerHour mph = Speed (round (mph * 1609344.0))
knots :: Double -> Speed
knots kt = Speed (round (kt * 1852000.0))
feetPerSecond :: Double -> Speed
feetPerSecond fps = Speed (round (fps * 1097280.0))
read :: String -> Maybe Speed
read s = readMaybe s :: (Maybe Speed)
toMetresPerSecond :: Speed -> Double
toMetresPerSecond (Speed s) = fromIntegral s / 3600000.0
toKilometresPerHour :: Speed -> Double
toKilometresPerHour (Speed s) = fromIntegral s / 1e+6
toMilesPerHour :: Speed -> Double
toMilesPerHour (Speed s) = fromIntegral s / 1609344.0
toKnots :: Speed -> Double
toKnots (Speed s) = fromIntegral s / 1852000.0
toFeetPerSecond :: Speed -> Double
toFeetPerSecond (Speed s) = fromIntegral s / 1097280.0
speed :: ReadP Speed
speed = do
s <- number
skipSpaces
u <- string "m/s" <|> string "km/h" <|> string "mph" <|> string "kt" <|> string "ft/s"
case u of
"m/s" -> return (metresPerSecond s)
"km/h" -> return (kilometresPerHour s)
"mph" -> return (milesPerHour s)
"kt" -> return (knots s)
"ft/s" -> return (feetPerSecond s)
_ -> pfail