module Data.Geo.Jord.Speed
(
Speed
, metresPerSecond
, kilometresPerHour
, milesPerHour
, knots
, feetPerSecond
, readSpeed
, readSpeedE
, readSpeedF
, toMetresPerSecond
, toKilometresPerHour
, toMilesPerHour
, toKnots
, toFeetPerSecond
) where
import Control.Applicative
import Control.Monad.Fail
import Data.Geo.Jord.Parser
import Data.Geo.Jord.Quantity
import Prelude hiding (fail)
import Text.ParserCombinators.ReadP
import Text.Read hiding (pfail)
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 Quantity Speed where
add a b = Speed (mmPerHour a + mmPerHour b)
sub a b = Speed (mmPerHour a - mmPerHour b)
zero = Speed 0
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))
readSpeed :: String -> Speed
readSpeed s = read s :: Speed
readSpeedE :: String -> Either String Speed
readSpeedE s =
case readMaybe s of
Nothing -> Left ("couldn't read speed " ++ s)
Just l -> Right l
readSpeedF :: (MonadFail m) => String -> m Speed
readSpeedF s =
let p = readEither s
in case p of
Left e -> fail e
Right l -> return l
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